apache java cache-control,Tomcat: Cache-Control

问题

Jetty has a CacheControl parameter (can be specified webdefault.xml) that determines the caching behavior of clients (by affecting headers sent to clients).

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?

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.

回答1:

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.

回答2:

Since Tomcat 7 there is a container provided expires filter that may help. See:

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_FilterExpiresFilter is a Java Servlet API port of Apache mod_expires. This filter controls the setting of the Expires HTTP header and the max-age directive of the Cache-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

org.apache.catalina.filters.ExpiresFilter

ExpiresByType image

access plus 10 days

ExpiresByType text/css

access plus 10 hours

ExpiresByType application/javascript

access plus 10 minutes

ExpiresDefault

access plus 0 seconds

ExpiresFilter

/*

REQUEST

回答3:

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

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.

SetCacheControl

ch.dietpizza.cacheControlFilter

SetCacheControl

/*

回答4:

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.html for example.

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:

回答5:

may be this what you are looking for :

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.

回答6:

The only param I know of is disableProxyCaching on elements. See here.

来源:https://stackoverflow.com/questions/2876250/tomcat-cache-control

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值