50

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.

7 Answers 7

45

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

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>
3
  • 4
    Can ExpiresFilter be used to set cache-control to "no-cache" and "no-store" at the same time? If yes, how do you achieve it in above code? This'd be great help.
    – Arjun
    Mar 13, 2015 at 10:08
  • 5
    I don't think so. From the documentation: To modify Cache-Control directives other than max-age (see RFC 2616 section 14.9), you can use other servlet filters or Apache Httpd mod_headers module.
    – Jack
    Mar 14, 2015 at 3:49
  • I ported the ExpiresFilter to Tomcat 6. See github.com/bnegrao/ExpiresFilter Apr 25, 2017 at 21:13
41

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.

3
  • This solved the problem for me. Safari 6 (iOS) caches POST requests and this was killing me! Thanks!
    – cbmeeks
    Nov 6, 2012 at 20:55
  • @whitey I am seeing Cache-control: private is added by default in my response. I am using tomcat as container. Can you help? Oct 22, 2019 at 20:05
  • System.currentTimeMillis() is faster and less garbage collection than new Date().getTime() according to stackoverflow.com/a/16249959/987044
    – ledlogic
    Nov 19, 2019 at 5:21
19

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>
1
  • Will this apply only to servlets, or also to static files, e.g css / images, which are served by Tomcat?
    – Ali
    May 6, 2014 at 16:37
1

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" />
0

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.

3
  • Thanks, but this does not help me. The parameter you're referring to affects 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. And, I want to do it for all resources at once.
    – Itay Maman
    May 20, 2010 at 17:46
  • ah ok..afaik implementing a filter is one way of doing it.
    – Inv3r53
    May 20, 2010 at 18:06
  • Seems like There is not built-in cache control in Tomcat.
    – Inv3r53
    May 20, 2010 at 18:20
0

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

0

Almost all answers I researched are from server side, actually client side needs to be worked on as well. In jsp/html need to add below in header.

<META http-equiv="Expires" CONTENT="0">  
<META http-equiv="Cache-Control" CONTENT="no-cache">  
<META http-equiv="Pragma" CONTENT="no-cache">  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.