How do I make Tomcat stop caching my servlet respo

2019-03-27 17:07发布

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!

5条回答
祖国的老花朵
2楼-- · 2019-03-27 17:07

The advice from Adeel Ansari is flawed: you should never modify CATALINA_HOME/conf/context.xml with webapp-specific configuration. That's what your-webapp/META-INF/context.xml is for.

You should also never specify the "docBase" attribute of <Context>. You should also never specify the "path" attribute of <Context>.

The OP has several options:

  1. Use the manager to reload the web app (undeploy/redeploy should not be necessary: a simple reload should work) ( http://tomcat.apache.org/tomcat-6.0-doc/manager-howto.html#Reload_An_Existing_Application )

  2. Set the element in META-INF/context.xml to have reloadable="true" ( http://tomcat.apache.org/tomcat-6.0-doc/config/context.html )

With all due respect to SO, if you need help with Tomcat, join the users' mailing list and get some real answers.

查看更多
狗以群分
3楼-- · 2019-03-27 17:07

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.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-03-27 17:22

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.

查看更多
做个烂人
5楼-- · 2019-03-27 17:28

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.

查看更多
相关推荐>>
6楼-- · 2019-03-27 17:32

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.

查看更多
登录 后发表回答