开发者

Tomcat: Cache-Control

Jetty has a CacheControl parameter (can be specified webdefault.xml) that determines the caching behavior of clients (by affecting headers sen开发者_开发技巧t 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.


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

  • Tomcat 10: https://tomcat.apache.org/tomcat-10.0-doc/config/filter.html#Expires_Filter
  • Tomcat 9: https://tomcat.apache.org/tomcat-9.0-doc/config/filter.html#Expires_Filter
  • Tomcat 8: https://tomcat.apache.org/tomcat-8.0-doc/config/filter.html#Expires_Filter
  • Tomcat 7: https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Expires_Filter
  • Tomcat 6 (unofficial backport): https://github.com/bnegrao/ExpiresFilter

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

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


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.


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.

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


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:

<Valve className="org.apache.catalina.authenticator.FormAuthenticator" securePagesWithPragma="false" />
<Valve className="org.apache.catalina.authenticator.NonLoginAuthenticator" securePagesWithPragma="false" />


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜