Recompile jsp with java classes?

J

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?
 
L

Last Timer

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).
 
H

Heiner Kücker

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
 
R

Ryan Stewart

JGH said:
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".
 
J

JGH

Ryan Stewart said:
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.
 
R

Ryan Stewart

JGH said:
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.
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.
 
J

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.


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Regarding Jsp And Java 1
JSP 0
Java with JSON file 3
Java RMI questions and MyEclipse 8
Tomcat won't recompile servlet 2
Newbie - JSP problem. 4
Java 1
Tomcat JSP importing class error 7

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top