java 如何让 Tomcat 停止缓存我的 servlet 响应?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/415520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:17:46  来源:igfitidea点击:

How do I make Tomcat stop caching my servlet responses?

javatomcatservletscaching

提问by

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.

我正在学习 Servlets 编程,在 Ubuntu 8.10 机器上使用 Apache Tomcat 6,但我遇到了一个非常烦人的问题——显然,与缓存有关。

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.

这就是我正在做的事情:我编写了一个 servlet,将它放在一个很好的目录结构中,然后使用 Tomcat Web 应用程序管理器部署它。它按预期工作。然后我编辑 servlet,重新编译并尝试再次访问它,但 Tomcat 一直返回相同的旧版本。重新加载应用程序甚至重新启动服务器都不起作用。唯一有效的是“取消部署”应用程序,然后重新部署它。

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!

我确定有办法解决这个问题,但我在网络上的任何地方都找不到答案(我确实搜索了很多)。我真的很感激任何帮助。谢谢!

回答by Christopher Schultz

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.

Adeel Ansari 的建议是有缺陷的:你不应该用 webapp 特定的配置修改 CATALINA_HOME/conf/context.xml。这就是 your-webapp/META-INF/context.xml 的用途。

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

您也不应该指定 <Context> 的“docBase”属性。您也不应该指定 <Context> 的“路径”属性。

The OP has several options:

OP 有几个选项:

  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)

  1. 使用管理器重新加载 Web 应用程序(不需要取消部署/重新部署:简单的重新加载应该可以)(http://tomcat.apache.org/tomcat-6.0-doc/manager-howto.html#Reload_An_Existing_Application

  2. 将 META-INF/context.xml 中的元素设置为 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.

恕我直言,如果您需要有关 Tomcat 的帮助,请加入用户的邮件列表并获得一些真正的答案。

回答by Adeel Ansari

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

在您的 TOMCAT_HOME/conf/ 下,您将找到一个名为 Context.xml 的文件。内容如下所示,

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

这两行都在这里取消注释,您也应该取消注释。您可能会收到第二条评论,或者根本没有评论。尝试取消注释,或在后一种情况下添加它。我假设您正在 TOMCAT_HOME/webapps 下部署您的应用程序。

[Edited]

[编辑]

Try using docBase, and pathattribure under your Contextelement. Below is the example

尝试使用docBaseContext元素下的路径属性。下面是例子

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

NOTE: Don't include WEB_INF

注意:不要包含 WEB_INF

[Edited]

[编辑]

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

可能是我错过了一些东西。看看这个。它是相同的,但更清晰和描述性更强,包括很少的其他选项。

回答by Brian Matthews

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.

我在 Tomcat 5.5 上也遇到过类似的问题。我从来没有弄清楚根本原因,但我通过从 %CATALINA_HOME%/work/Catalina/localhost 中删除与 webapp 对应的文件夹来解决它。这不是一个很好的解决方案,但它避免了您必须取消部署/重新部署整个应用程序。

回答by Olaf Kock

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.

你不会说你是使用 ubuntu tomcat 还是从tomcat.apache.org单独下载。如果您使用的是 ubuntu,请尝试使用单独的下载使其更简单。标准下载非常易于管理,而且非常适合开箱即用。可能(我不知道)ubuntu 的配置可能更多地用于生产用途,例如它可能会有所加强。

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.

推荐的 tomcat 生产设置正是您所描述的(例如,没有自动部署等)。开发设置更易于使用。

回答by Sarel Botha

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.

如果您使用 Netbeans,那么它会在您保存文件时自动重新编译该类并将其注入正在运行的 web 应用程序中。没有涉及额外的步骤,只需点击保存。