Recompile jsp with java classes?

Discussion in 'Java' started by JGH, Feb 7, 2005.

  1. JGH

    JGH Guest

    I created some java classes on a tomcat server. Now I want to use them in a
    jsp page. I copied the .class files to $CATALINA_HOME/webapps/ROOT/WEB-
    INF/classes/ and was able to access them.

    However, the server doesn't seem to notice if I make a change to one of the
    ..class files. I have to make a change to the .jsp page that uses the
    class. I've gotten in the habit of adding a space and then deleting it and
    then saving the source code for the .jsp page each time I make a change to
    one of the .java files and recompile to create a new .class file.

    I tried putting the .java files in the .../classes directory because I had
    read that this would work. But I got an error message that said my classes
    were missing.

    Besides, I don't want to put my source code in the tomcat space. When I'm
    done with it, maybe. But I'm still working on it and I want it in a subdir
    of my own home directory.

    So I guess this is a 2 part question: How do you manage your source code?
    And how do I get tomcat to recognize that I've changed a .class file?
     
    JGH, Feb 7, 2005
    #1
    1. Advertising

  2. JGH

    Last Timer Guest

    The source can reside any where.
    Use a jar file to in <Tomcat-Root>/webapps/app/lib path. You may have
    to restart Tomcat to recognize the changes in the jar file (or the
    classes).
     
    Last Timer, Feb 7, 2005
    #2
    1. Advertising

  3. JGH
    > I created some java classes on a tomcat server. Now I want to use them in a
    > jsp page. I copied the .class files to $CATALINA_HOME/webapps/ROOT/WEB-
    > INF/classes/ and was able to access them.
    >
    > However, the server doesn't seem to notice if I make a change to one of the
    > .class files. I have to make a change to the .jsp page that uses the
    > class. I've gotten in the habit of adding a space and then deleting it and
    > then saving the source code for the .jsp page each time I make a change to
    > one of the .java files and recompile to create a new .class file.
    >
    > I tried putting the .java files in the .../classes directory because I had
    > read that this would work. But I got an error message that said my classes
    > were missing.
    >
    > Besides, I don't want to put my source code in the tomcat space. When I'm
    > done with it, maybe. But I'm still working on it and I want it in a subdir
    > of my own home directory.
    >
    > So I guess this is a 2 part question: How do you manage your source code?
    > And how do I get tomcat to recognize that I've changed a .class file?
    >


    Clear the work directory of your Tomcat
    to force recompiling JSP.

    Heiner Kuecker
    Internet: http://www.heinerkuecker.de http://www.heiner-kuecker.de
    JSP WorkFlow PageFlow Page Flow FlowControl Navigation: http://www.control-and-command.de
    Java Expression Formula Parser: http://www.heinerkuecker.de/Expression.html
    CnC Template Technology http://www.heinerkuecker.de/Expression.html#templ
    Domain Specific Languages http://www.heinerkuecker.de/DomainParser.html
     
    Heiner Kücker, Feb 7, 2005
    #3
  4. JGH

    Ryan Stewart Guest

    "JGH" <> wrote in message
    news:cu8d8b$50$...
    >I created some java classes on a tomcat server. Now I want to use them in a
    > jsp page. I copied the .class files to $CATALINA_HOME/webapps/ROOT/WEB-
    > INF/classes/ and was able to access them.
    >
    > However, the server doesn't seem to notice if I make a change to one of the
    > .class files. I have to make a change to the .jsp page that uses the
    > class. I've gotten in the habit of adding a space and then deleting it and
    > then saving the source code for the .jsp page each time I make a change to
    > one of the .java files and recompile to create a new .class file.
    >
    > I tried putting the .java files in the .../classes directory because I had
    > read that this would work. But I got an error message that said my classes
    > were missing.

    No. Tomcat will not compile your classes for you.

    > Besides, I don't want to put my source code in the tomcat space. When I'm
    > done with it, maybe. But I'm still working on it and I want it in a subdir
    > of my own home directory.
    >
    > So I guess this is a 2 part question: How do you manage your source code?

    Part of my typical directory structure looks like:
    src
    java
    webapp

    "java" is my .java files, and "webapp" holds JSPs, WEB-INF contents besides
    classes and JARS, etc.

    > And how do I get tomcat to recognize that I've changed a .class file?

    First, from what you said above, it sounds like you're using the default context
    to run your webapp. I don't know anything about configuring that. I don't know
    how similar it is to configuring other contexts either. For a normal context, it
    depends somewhat on what version of Tomcat you're running. Ultimately, in any
    (recent) version, there is a "Context" element in an XML configuration file
    somewhere, either in $CATALINA_HOME/conf/server.xml for Tomcat 4 (and 5 if going
    against Jakarta's suggestion) or in an individual, smaller XML file in
    $CATALINA_HOME/conf/Catalina/localhost, assuming a default setup. A basic
    Context element looks like this:
    <Context docBase="C:\Projects\publicExamples\target" path="examples">
    </Context>

    For Tomcat 5, that would go in a file named "examples.xml", and I believe the
    "path" attribute is optional and it will take its name from the file, but I
    don't know that for sure. What this does is map http://localhost:8080/examples
    to the folder "C:\Projects\publicExamples\target". "examples" is your context.
    Now, to answer your question, add an attribute to your Context element:
    reloadable="true"

    That causes Tomcat to watch for changes to class files, web.xml, and
    META-INF/context.xml. It will be the main thread (I think) monitoring for
    changes when it has time. If you want to spawn a new thread devoted to watching
    for changes, add another attribute:
    backgroundProcessorDelay="2"

    That causes the new thread to check for changes every 2 seconds. For more
    details on setting up contexts and Tomcat in general, see:
    http://jakarta.apache.org/tomcat/index.html

    Click your Tomcat version on the left under "Documentation", then on the next
    page click "Tomcat Configuration" under "Reference".
     
    Ryan Stewart, Feb 8, 2005
    #4
  5. JGH

    JGH Guest

    "Ryan Stewart" <> wrote in
    > Part of my typical directory structure looks like:
    > src
    > java
    > webapp
    >
    > "java" is my .java files, and "webapp" holds JSPs, WEB-INF contents
    > besides classes and JARS, etc.



    How do you then get the tomcat server to know where your .class files
    are?

    > For Tomcat 5, that would go in a file named "examples.xml", and I
    > believe the "path" attribute is optional and it will take its name
    > from the file, but I don't know that for sure. What this does is map
    > http://localhost:8080/examples to the folder
    > "C:\Projects\publicExamples\target". "examples" is your context. Now,
    > to answer your question, add an attribute to your Context element:
    > reloadable="true"
    >



    I can't seem to get this to work.

    I created a file in $CATALINA_HOME/conf/ named newapp.xml. Contents are
    as follows:

    <Context
    path="/newapp"
    docBase="/home/john/newapp"
    debug="0"
    reloadable="true"
    cachingAllowed="false"
    backgroundProcessorDelay="2"
    >

    </Context>


    This did indeed make tomcat get the homepage for the app from a folder
    in my home directory. So I know the .xml file is being read and used.
    However, it seems to have no effect otherwise. It's still looking for
    classes in $CATALINA_HOME/webapps/ROOT/WEB-INF/classes/. And if I change
    a file in there, it doesn't reload it unless I restart tomcat.

    I can actually delete a file from $CATALINA_HOME/webapps/ROOT/WEB-
    INF/classes, re-run the jsp page, and get an error. Then, if I put a new
    version of the .class file back, the *old* class is run. However, if
    I do nothing else but restart the tomcat server, finally, the new class
    is used.

    I've checked it again and again and I am about as sure as a human being
    can be that this is what's happening. That's why I added the
    cachingAllowed="false" line to the config .xml file.
     
    JGH, Feb 8, 2005
    #5
  6. JGH

    Ryan Stewart Guest

    "JGH" <> wrote in message
    news:cub6o2$faa$...
    > "Ryan Stewart" <> wrote in
    >> Part of my typical directory structure looks like:
    >> src
    >> java
    >> webapp
    >>
    >> "java" is my .java files, and "webapp" holds JSPs, WEB-INF contents
    >> besides classes and JARS, etc.

    >
    > How do you then get the tomcat server to know where your .class files
    > are?
    >

    I use Ant (http://ant.apache.org/), Maven (http://maven.apache.org/), or my IDE
    build utility to create the proper webapp directory structure.

    >> For Tomcat 5, that would go in a file named "examples.xml", and I
    >> believe the "path" attribute is optional and it will take its name
    >> from the file, but I don't know that for sure. What this does is map
    >> http://localhost:8080/examples to the folder
    >> "C:\Projects\publicExamples\target". "examples" is your context. Now,
    >> to answer your question, add an attribute to your Context element:
    >> reloadable="true"
    >>

    > I can't seem to get this to work.
    >
    > I created a file in $CATALINA_HOME/conf/ named newapp.xml. Contents are
    > as follows:
    >

    You need to reread my original post. If you're using Tomcat 5, your newapp.xml
    goes in $CATALINA_HOME/conf/Catalina/localhost, *not* just in /conf. And if
    you're using Tomcat 4, you can't use this approach.

    > <Context
    > path="/newapp"
    > docBase="/home/john/newapp"

    I assume you're using Unix/Linux? I don't know much about those OSs, so can't
    tell you if this is correct. In Windows, it needs to be a fully qualified path
    or a relative path which is relative to "the appBase directory of the owning
    Host", according to the docs. I always use absolute paths.

    > debug="0"

    The default value is 0, so this doesn't really do anything.

    > reloadable="true"
    > cachingAllowed="false"
    > backgroundProcessorDelay="2"
    > >

    > </Context>

    The rest of that looks okay, if there's no typos that we both missed.

    > This did indeed make tomcat get the homepage for the app from a folder
    > in my home directory. So I know the .xml file is being read and used.
    > However, it seems to have no effect otherwise. It's still looking for
    > classes in $CATALINA_HOME/webapps/ROOT/WEB-INF/classes/. And if I change
    > a file in there, it doesn't reload it unless I restart tomcat.
    >
    > I can actually delete a file from $CATALINA_HOME/webapps/ROOT/WEB-
    > INF/classes, re-run the jsp page, and get an error. Then, if I put a new
    > version of the .class file back, the *old* class is run. However, if
    > I do nothing else but restart the tomcat server, finally, the new class
    > is used.
    >
    > I've checked it again and again and I am about as sure as a human being
    > can be that this is what's happening. That's why I added the
    > cachingAllowed="false" line to the config .xml file.
    >


    You may be experiencing some residual of something else you tried, because the
    setup you described above should not work at all. The XML file needs to be in
    the directory I mentioned above.
     
    Ryan Stewart, Feb 9, 2005
    #6
  7. JGH

    JGH Guest

    JGH <> wrote in
    news:cu8d8b$50$:

    > I created some java classes on a tomcat server. Now I want to use them
    > in a jsp page. I copied the .class files to
    > $CATALINA_HOME/webapps/ROOT/WEB- INF/classes/ and was able to access
    > them.
    >
    > However, the server doesn't seem to notice if I make a change to one
    > of the .class files. I have to make a change to the .jsp page that
    > uses the class. I've gotten in the habit of adding a space and then
    > deleting it and then saving the source code for the .jsp page each
    > time I make a change to one of the .java files and recompile to create
    > a new .class file.
    >



    Replying to my own post here because I think I found the definitive
    answer. -- ant.

    There is documentation on the apache web site on how to organize your
    code and how to get make tomcat reload classes when you change them.
    Here is the URL I've been working from:

    http://jakarta.apache.org/tomcat/tomcat-5.0-doc/appdev/source.html
     
    JGH, Feb 23, 2005
    #7
    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. Replies:
    0
    Views:
    4,472
  2. Replies:
    2
    Views:
    531
  3. Sanny
    Replies:
    30
    Views:
    1,416
    Nick Keighley
    Feb 3, 2010
  4. Sanny
    Replies:
    34
    Views:
    1,154
    Nick Keighley
    Feb 3, 2010
  5. Paul Cager
    Replies:
    0
    Views:
    426
    Paul Cager
    Jan 29, 2010
Loading...

Share This Page