my first bean (tomcat)

Discussion in 'Java' started by ian ward, Jun 10, 2005.

  1. ian ward

    ian ward Guest

    Hello,
    I'm getting more and more confused looking at old threads (and some are
    very old) so I'll take the liberty of a new post for a very basic
    question.

    Why am I having a problem accessing my java class/bean from a jsp page
    (which itself is running ok)? I'm running Tomcat 5 so I believe I need
    a "myapp.xml" file in conf\catalina\localhost\ containing a context
    element whose purpose is to associate an http call with the path of the
    home directory where the class can be found (in \classes or in a jar in
    \lib).

    Then, in theory, 'jsp:usebean..etc.' will find the class I want.
    Instead I get a stack trace including lines like this...

    org.apache.jasper.JasperException: /jsp/listProgrammes.jsp(5,0)
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:409)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:150)
    org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1227)
    org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1116)
    org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)

    (the top line is my jsp page which is trying to get hold of the bean)

    Can anyone spell out the principles for me while I go away and check
    for typos again? (Yes I have read some principles as explained in the
    Tomcat docs but, well, it doesn't help for the moment.)

    Many thanks
    Ian
     
    ian ward, Jun 10, 2005
    #1
    1. Advertising

  2. ian ward wrote:

    > I'm getting more and more confused looking at old threads (and some are
    > very old) so I'll take the liberty of a new post for a very basic
    > question.


    Looking at very old threads along side more recent ones is bound to get
    you confused because the technology changes over time.

    > Why am I having a problem accessing my java class/bean from a jsp page
    > (which itself is running ok)? I'm running Tomcat 5 so I believe I need
    > a "myapp.xml" file in conf\catalina\localhost\ containing a context
    > element whose purpose is to associate an http call with the path of the
    > home directory where the class can be found (in \classes or in a jar in
    > \lib).


    JSP pages, servlets, and associated classes are aggregated together into
    modular units called "Web Applications" (often referred to as
    "webapps"). Before you can go anywhere, you need to assimilate the
    basics of web applications and the specifics of Tomcat in that area.
    The Tomcat docs would be a good place to start. Try

    http://jakarta.apache.org/tomcat/tomcat-5.0-doc/appdev/deployment.html

    > Then, in theory, 'jsp:usebean..etc.' will find the class I want.
    > Instead I get a stack trace including lines like this...
    >
    > org.apache.jasper.JasperException: /jsp/listProgrammes.jsp(5,0)
    > org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)
    > org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:409)
    > org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:150)
    > org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1227)
    > org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1116)
    > org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
    >
    > (the top line is my jsp page which is trying to get hold of the bean)
    >
    > Can anyone spell out the principles for me while I go away and check
    > for typos again? (Yes I have read some principles as explained in the
    > Tomcat docs but, well, it doesn't help for the moment.)


    JSP pages are translated into Java code, which is then compiled into a
    servlet. Jasper is Tomcat's tool for the JSP -> Java step. Your stack
    trace appears to indicate that Jasper encountered an error trying to
    parse the JSP file; that probably means a syntax error in your code, and
    (5,0) is probably a location close to the error. I'm not certain
    offhand whether it means line 5 character 0 or character 5 line 0.

    You should also be aware that like XML tags but unlike HTML tags, JSP
    tags are case-sensitive. The correct spelling, therefore, is either
    <jsp:useBean .../> or <jsp:useBean ...>...</jsp:useBean>. To get more
    specific help, you'll need to show us the code that Jasper fails on.

    --
    John Bollinger
     
    John C. Bollinger, Jun 10, 2005
    #2
    1. Advertising

  3. ian ward

    ian ward Guest

    John,

    thanks for your reply. I may have come over a bit more
    helpless/clueless than I am - I hope! I have looked at the Tomcat page
    you indicate and I do have some idea about the architecture of a webapp
    and where things should go but I am a bit concerned about some of the
    other files which need setting. I am finding the doc a bit wordy to
    wade thru and I'd like to see a 'recipe' for getting a Bean (capital
    'b' in useBean, I checked) going, the main steps, 'cos I seem to be
    missing/hashing one of them. By the way, are you sure I've got a syntax
    kind of error in my jsp? the offending line is simple enough.....

    <jsp:useBean id="dialogueSystem" scope="request"
    class="controllers.impl.C_SystemImpl" />

    .....my impression is that it can't interprete it 'cos it can't find the
    thing and it can't find the thing 'cos it's relying on information in
    another file which isn't correct. Example, here is the contents of my
    myapp.xml file in conf\Catalina\localhost\

    <Context path="/myapp" docBase="C:/jakarta-tomcat-5.0.28/webapps/myapp"
    debug="0" reloadable="true">

    </Context>

    ....oh yeah, I get a bit confused with which way the obliques should go
    as well (yes, I'm on Windows)!
    Last thought, I've noticed that the jsp-examples don't seem to need any
    of this stuff - maybe I'm chasing a red herring, even though it is
    emphasised on one those Tomcat doc pages!

    Ian
     
    ian ward, Jun 10, 2005
    #3
  4. ian ward wrote:

    > thanks for your reply. I may have come over a bit more
    > helpless/clueless than I am - I hope! I have looked at the Tomcat page
    > you indicate and I do have some idea about the architecture of a webapp
    > and where things should go but I am a bit concerned about some of the
    > other files which need setting. I am finding the doc a bit wordy to
    > wade thru and I'd like to see a 'recipe' for getting a Bean (capital
    > 'b' in useBean, I checked) going, the main steps, 'cos I seem to be
    > missing/hashing one of them.


    Recipe:

    1) Write Java source code for your bean. It should have a no-arg
    constructor, and, technically, it should be Serializable. Make sure to
    assign the bean to a named package.

    2) Compile the bean's Java code to get a class file for it.

    3) Put the bean's class file in the webapp's WEB-INF/classes directory,
    or jar it up and put the jar in the webapp's WEB-INF/lib directory. In
    either case, make sure the path (within WEB-INF/classes or within the
    jar) maps correctly onto the bean's package name.

    4) Use a <jsp:useBean .../> action to declare your JSP's use of a bean
    instance; depending on the bean's scope and the application flow the
    instance may or may not be newly created for any particular page view.

    > By the way, are you sure I've got a syntax
    > kind of error in my jsp? the offending line is simple enough.....
    >
    > <jsp:useBean id="dialogueSystem" scope="request"
    > class="controllers.impl.C_SystemImpl" />


    No, I'm not *sure* that you have a syntax error, but I think it highly
    likely based on the stack trace. It is possible, however, that the
    error is not in your useBean action itself but rather in something else
    nearby in the JSP source.

    > ....my impression is that it can't interprete it 'cos it can't find the
    > thing and it can't find the thing 'cos it's relying on information in
    > another file which isn't correct.


    The page translator -- which is where the error appears to occur -- does
    not need to find the class file for your bean. It simply uses the
    useBean attributes to produce some Java source code. If the class file
    is out of place then you should get a different error from a different
    component (details and timing depend on the servlet container).

    > Example, here is the contents of my
    > myapp.xml file in conf\Catalina\localhost\
    >
    > <Context path="/myapp" docBase="C:/jakarta-tomcat-5.0.28/webapps/myapp"
    > debug="0" reloadable="true">
    >
    > </Context>


    As far as I can tell, this whole myapp.xml business is unnecessary with
    Tomcat 5. I don't know whether Tomcat does anything with that file at
    all if it is present. For the time being, I'd remove it altogether and
    see what happens; unless Tomcat starts having trouble finding your
    webapp, you can probably keep it gone.

    > ...oh yeah, I get a bit confused with which way the obliques should go
    > as well (yes, I'm on Windows)!


    The way you have put them should work.

    > Last thought, I've noticed that the jsp-examples don't seem to need any
    > of this stuff - maybe I'm chasing a red herring, even though it is
    > emphasised on one those Tomcat doc pages!


    For which version of Tomcat? I don't see anything about it on the page
    I pointed you to, and it certainly ought to be there if the file is
    necessary.

    --
    John Bollinger
     
    John C. Bollinger, Jun 10, 2005
    #4
  5. ian ward

    ian ward Guest

    John,
    Very many thanks for the recipe. It looks reassuringly/worryingly
    familiar!
    Good point about misleading line numbers ie the error could be
    somewhere else...but as I have had the useBean line in different places
    with no change I'll not get my hopes up. It is true that if I'm in an
    unfamiliar area of work then I tend to opt for the most complex
    diagnosis and it's often a typo or something similar.

    Maybe your point 3 needs looking at more carefully, I was pretty sure I
    had that covered...however, off the top of my head I'm not sure how I
    verify it...I'll try to work on that, it may be the most promising.

    Finally - I found all the stuff about context here......
    C:\jakarta-tomcat-5.0.28\webapps\tomcat-docs\config\context.html.....but
    I don't remember anymore why I started looking for it....oh, yeah, I've
    got a book which mentions it (although it's an oldish book and, as this
    page also mentions in passing, it talks about putting the context
    reference in the server.xml file which is old hat apparently).
    But, as you say, it maybe IS a red herring and that's why I hope your
    recipe works and I don't have to go wading through more
    unnecessary/misleading/confusing doc!!

    Thanks again
    Ian
     
    ian ward, Jun 10, 2005
    #5
  6. ian ward

    ian ward Guest

    Hello John (or anyone else who happens, like me, to be working on
    Sunday),

    Looking at point 3 from your mail, as I said I would in my previous
    mail, I believe I have a problem because the package name of my Bean
    has two levels. I have found this by including into my webapp a Bean I
    know works (numguess from the tomcat jsp-examples folder) in its own
    package. Then I put a version of the Bean in one of my packages, still
    ok and then I put a version in a subpackage at which point I get a
    problem.

    Is this normal, explainable, avoidable?

    Thanks
    Ian
     
    ian ward, Jun 12, 2005
    #6
  7. ian ward wrote:

    > Looking at point 3 from your mail, as I said I would in my previous
    > mail, I believe I have a problem because the package name of my Bean
    > has two levels. I have found this by including into my webapp a Bean I
    > know works (numguess from the tomcat jsp-examples folder) in its own
    > package. Then I put a version of the Bean in one of my packages, still
    > ok and then I put a version in a subpackage at which point I get a
    > problem.
    >
    > Is this normal, explainable, avoidable?


    No, maybe, probably.

    Point #3, reprise:

    3) Put the bean's class file in the webapp's WEB-INF/classes directory,
    or jar it up and put the jar in the webapp's WEB-INF/lib directory. In
    either case, make sure the path (within WEB-INF/classes or within the
    jar) maps correctly onto the bean's package name.


    So to be as specific as possible, given a bean (or any other class used
    by your webapp) with fully-qualified class name org.foo.Bar, you can do
    either one of these:

    a) put Bar.class in directory WEB-INF/classes/org/foo/

    or

    b) create a jar <somename>.jar containing the class file as an entry
    named org/foo/Bar.class, then put the jar in WEB-INF/lib/.

    You may of course put many class files into the same jar file. You may
    put multiple jar files into WEB-INF/lib/, and multiple class files into
    WEB-INF/classes/. It is best to avoid putting the same class in both
    places, though technically it isn't an error.

    I assure you that this works for many, many people, including me, with
    package names containing various numbers of components, on a wide
    variety of application servers including Tomcat 5. If it is not working
    for you then the only possibilities I see are an erroneous JSP page
    (more likely), a bad or misplaced class file (doesn't seem to fit the
    facts), or a broken Tomcat installation (not inconceivable).

    --
    John Bollinger
     
    John C. Bollinger, Jun 13, 2005
    #7
  8. ian ward

    ian ward Guest

    John,
    well, it seems to work for me now, I'm happy to say, but I'm not sure
    why!
    I fiddled a bit, and the last fiddle I did (although I'm not sure I
    didn't already fiddle this but maybe something else was out of place at
    the time) was to change the scope of the useBean action from session to
    request and from then on it behaved itself, even if I changed it back
    to session.
    Thanks for your clear and explicit replies, I'll know where to come for
    the next prob :)
    Ian
     
    ian ward, Jun 14, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Benjamin Stewart

    To bean or not to bean...

    Benjamin Stewart, Jun 30, 2003, in forum: Java
    Replies:
    0
    Views:
    808
    Benjamin Stewart
    Jun 30, 2003
  2. David Thielen
    Replies:
    2
    Views:
    799
    Marek Lange
    Sep 12, 2003
  3. Markku Salminen
    Replies:
    3
    Views:
    5,855
    Michael Borgwardt
    Jan 21, 2004
  4. Takeshi
    Replies:
    0
    Views:
    510
    Takeshi
    Jan 28, 2004
  5. kumar
    Replies:
    1
    Views:
    547
    Wendy S
    Feb 13, 2004
Loading...

Share This Page