views:

4397

answers:

4

I'm learning Servlets programming, using Apache Tomcat 6 on a Ubuntu 8.10 machine, and I'm running with a very annoying issue -- apparently, related to caching.

This is what I'm doing: I write a servlet, put it in a nice directory structure and deploy it using the Tomcat Web Application Manager. It works as expected. Then I edit the servlet, recompile and try to access it again, but Tomcat keeps returning the same old version. Reloading the Application or even restarting the server does not work. The only thing that works is "Undeploying" the Application, then deploying it all over again.

I have to do this every single time I make any small change on my code. It sucks.

I'm sure there is a way around this, but I couldn't find the answer anywhere on the web (and I did search a lot). I would really appreciate any help. Thanks!

+2  A: 

Under your TOMCAT_HOME/conf/, you will find a file named Context.xml. The content would look like below,

<Context>
    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/classes</WatchedResource>
</Context>

Both lines are uncommented here, you should uncomment both too. Its likely that you will have the 2nd one commented or might not have it at all. Try uncomment it, or add it in latter case. I am assuming you are deploying your app under TOMCAT_HOME/webapps.

[Edited]

Try using docBase, and path attribure under your Context element. Below is the example

<Context docBase="path-to-WEB-INF" path="/your-app">

NOTE: Don't include WEB_INF

[Edited]

May be I am missing something. Check this out. Its the same, but much more clear and descriptive including few other options.

Adeel Ansari
I found context.xml at CATALINA_BASE/conf/ (the default homepage on localhost:8080 tells me about CATALINA_HOME and CATALINA_BASE, I'm not sure if that's the same as TOMCAT_HOME). Anyway, I added the second line, restarted the server, but with no results :-(
BTW, I'm using the Web Application Manager to deploy (and undeploy) it -- as I understand it, it is going under CATALINA_BASE/webapps.
Yes, CATALINA_HOME or BASE is the same. Are you using netbeans? Do you have a separate context file for your app? I mean <your-app>.xml
Adeel Ansari
I'm not using Netbeans. The only XML file I have is web.xml.
Have you tried my addendum?
Adeel Ansari
It just tried it, still doesn't work. I'll read the docs on the link you provided, see if I can figure it out.
I tried this and it worked for me. Added a delete task as part of my ant task that deploys to tomcat, and it works a treat.
Brian Warshaw
+1  A: 

I have encountered similar problems with Tomcat 5.5. I never figured out the root cause but I worked around it by deleting the folder corresponding to the webapp from %CATALINA_HOME%/work/Catalina/localhost. Its not a great solution but it avoids you having to undeploy/redeploy your whole application.

bmatthews68
That will work indeed. Actually that directory works a cache. So, deleting that, will solve the problem. But sometimes you mind deleting it every time, you test.
Adeel Ansari
Actually I had already seen this tip somewhere else, but it didn't work for me. CATALINA_HOME/work/Catalina/localhost/my_project_name is just an empty directory here, and nothing changes if I delete it.
It might be caching things somewhere else. Or are you not having any JSPs or never requested any for the time being. Because /work/Catalina/localhost/... is a directory compiled JSPs are kept, not the servlets or other classes.
Adeel Ansari
A: 

If you use Netbeans then it automatically recompiles the class and injects it into the running webapp when you save the file. There are no additional steps involved, just hit save.

sjbotha
No, it wouldn't. You need to build it and then re-deploy it, in order to see the change. And he/she doesn't want to re-deploy. Compiling the source is different and depends on IDE settings.
Adeel Ansari
+1  A: 

You don't say if you are using the ubuntu tomcat or a separate download from tomcat.apache.org. If you are using the ubuntu one, try to make it simpler with using a separate download. The standard download is very easy to administer and rather geared to working out of the box. It might be (I don't know) that the ubuntu one might be configured more towards production use, e.g. it might be somewhat hardened.

The recommended production setting for tomcat is just what you describe (e.g. no auto-deploy etc). The development setting is way easier to use.

Olaf