Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat: Cache-Control

Tags:

java

tomcat

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.

like image 415
Itay Maman Avatar asked May 20 '10 17:05

Itay Maman


People also ask

How do I stop Tomcat from caching?

xml you can change the value of cachingAllowed by removing the flag. Remember to delete the cache folder after that. Resources : Apache Tomcat Configuration Reference.

Does Tomcat have a cache?

Caching helps when you want to leverage files not being downloaded each time and serve it from cache.

What is Cache-Control used for?

Cache-control is an HTTP header used to specify browser caching policies in both client requests and server responses. Policies include how a resource is cached, where it's cached and its maximum age before expiring (i.e., time to live).

What is default cache-control?

The default cache-control header is : Private. A cache mechanism may cache this page in a private cache and resend it only to a single client. This is the default value. Most proxy servers will not cache pages with this setting.


3 Answers

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> 
like image 94
Jack Avatar answered Nov 04 '22 23:11

Jack


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.

like image 30
whitey Avatar answered Nov 04 '22 23:11

whitey


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>
like image 34
Stu Thompson Avatar answered Nov 05 '22 01:11

Stu Thompson