Java Tomcat:缓存控制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2876250/
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
Tomcat: Cache-Control
提问by Itay Maman
Jetty has a CacheControl parameter(can be specified webdefault.xml) that determines the caching behavior of clients (by affecting headers sent to clients).
Jetty 有一个CacheControl 参数(可以指定为 webdefault.xml),它决定了客户端的缓存行为(通过影响发送到客户端的标头)。
Does Tomcat has a similar option? In short, I want to turn off caching of all pages delivered by a tomcat server and/or by a specific webapp?
Tomcat 有类似的选项吗?简而言之,我想关闭由 tomcat 服务器和/或特定 webapp 提供的所有页面的缓存?
Update
更新
Please note that I am not referring to server-side caching. I want the server to tell all clients (browsers) not to use their own cache and to always fetch the content from the server. I want to do it for all resources, including static resources (.css, .js, etc.) at once.
请注意,我不是指服务器端缓存。我希望服务器告诉所有客户端(浏览器)不要使用他们自己的缓存并始终从服务器获取内容。我想一次为所有资源执行此操作,包括静态资源(.css、.js 等)。
采纳答案by whitey
Similar to the post above, except there are some issues with that code. This will disable all browser caching:
与上面的帖子类似,除了该代码存在一些问题。这将禁用所有浏览器缓存:
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
public class CacheControlFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse resp = (HttpServletResponse) response;
resp.setHeader("Expires", "Tue, 03 Jul 2001 06:00:00 GMT");
resp.setDateHeader("Last-Modified", new Date().getTime());
resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
resp.setHeader("Pragma", "no-cache");
chain.doFilter(request, response);
}
}
and then map in web.xml as described in Stu Thompson's answer.
然后按照Stu Thompson's answer 中的描述映射到 web.xml 中。
回答by Inv3r53
may be this what you are looking for :
可能这就是你要找的:
http://tomcat.apache.org/tomcat-6.0-doc/config/context.html#Context%20Parameters
http://tomcat.apache.org/tomcat-6.0-doc/config/context.html#Context%20Parameters
cachingAllowed : If the value of this flag is true, the cache for static
resources will be used. If not specified, the default value of the flag is true.
Also delete the application cache folder in /work/Catalina/localhost after changing this flag.
更改此标志后,还要删除 /work/Catalina/localhost 中的应用程序缓存文件夹。
回答by Stu Thompson
I don't believe there is a configuration to do this. But it should not be much of an effort to write a filter to set the Cache-Control header on a per webapp-basis. E.g.:
我不相信有配置可以做到这一点。但是编写一个过滤器来设置基于每个 webapp 的 Cache-Control 标头应该不会很费力。例如:
public class test implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(request, response);
((StatusResponse)response).setHeader("Cache-Control",
"max-age=0, private, must-revalidate");
}
public void destroy() {}
public void init(FilterConfig arg0) throws ServletException {}
}
And you'd place this snippet into your webapp's web.xml
file.
您可以将此代码段放入您的 web 应用web.xml
程序文件中。
<filter>
<filter-name>SetCacheControl</filter-name>
<filter-class>ch.dietpizza.cacheControlFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SetCacheControl</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
回答by Sean Owen
回答by CXJ
There actually are several elements in the Tomcat configuration which directly affect this. See documentation at http://tomcat.apache.org/tomcat-6.0-doc/config/valve.htmlfor example.
实际上,Tomcat 配置中有几个元素直接影响到这一点。例如,请参阅http://tomcat.apache.org/tomcat-6.0-doc/config/valve.html 上的文档。
Atlassian recommends the following two statements to ENABLE browser-side caching so that Microsoft Internet Explorer will be able to correctly download and view attached documents:
Atlassian 建议使用以下两条语句来启用浏览器端缓存,以便 Microsoft Internet Explorer 能够正确下载和查看附加文档:
<Valve className="org.apache.catalina.authenticator.FormAuthenticator" securePagesWithPragma="false" />
<Valve className="org.apache.catalina.authenticator.NonLoginAuthenticator" securePagesWithPragma="false" />
回答by Hyman
Since Tomcat 7 there is a container provided expires filter that may help. See:
由于 Tomcat 7 提供了一个容器,过期过滤器可能会有所帮助。看:
- https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Expires_Filter
- https://tomcat.apache.org/tomcat-8.0-doc/config/filter.html#Expires_Filter
- https://tomcat.apache.org/tomcat-9.0-doc/config/filter.html#Expires_Filter
- https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Expires_Filter
- https://tomcat.apache.org/tomcat-8.0-doc/config/filter.html#Expires_Filter
- https://tomcat.apache.org/tomcat-9.0-doc/config/filter.html#Expires_Filter
ExpiresFilter is a Java Servlet API port of Apache mod_expires. This filter controls the setting of the
Expires
HTTP header and themax-age
directive of theCache-Control
HTTP header in server responses. The expiration date can set to be relative to either the time the source file was last modified, or to the time of the client access.
ExpiresFilter 是Apache mod_expires的 Java Servlet API 端口。此过滤器控制
Expires
HTTP 标头的设置和服务器响应中 HTTP 标头的max-age
指令Cache-Control
。到期日期可以设置为相对于上次修改源文件的时间或客户端访问的时间。
<filter>
<filter-name>ExpiresFilter</filter-name>
<filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
<init-param>
<param-name>ExpiresByType image</param-name>
<param-value>access plus 10 days</param-value>
</init-param>
<init-param>
<param-name>ExpiresByType text/css</param-name>
<param-value>access plus 10 hours</param-value>
</init-param>
<init-param>
<param-name>ExpiresByType application/javascript</param-name>
<param-value>access plus 10 minutes</param-value>
</init-param>
<!-- Let everything else expire immediately -->
<init-param>
<param-name>ExpiresDefault</param-name>
<param-value>access plus 0 seconds</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>ExpiresFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>