4

I am trying to set the Cache Control for Tomcat 7 to the no-cache option.

I have tried to use the ExpiresFilter in my web.xml as follows:

<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 0 seconds</param-value>
    </init-param>
    <init-param>
    <param-name>ExpiresByType text/css</param-name>
    <param-value>access plus 0 seconds</param-value>
    </init-param>
    <init-param>
    <param-name>ExpiresByType application/javascript</param-name>
    <param-value>access plus 0 seconds</param-value>
    </init-param>

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

However when I read my response headers all I get is:

Cache-Control:max-age=0

and not Cache-Control: no-cache.

How can I set my Tomcat server to specify Cache-Control: no-cache only through configuration files?

jgr208
  • 2,896
  • 9
  • 36
  • 64
  • 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. – Federico Sierra Mar 08 '17 at 18:59
  • 1
    @FedericoSierra do you know what other servlet filters they are referring to? I didn't see any in the documentation that would work or do you have to make your own? – jgr208 Mar 08 '17 at 19:11
  • 1
    No, you can add a custom servlet (http://stackoverflow.com/questions/2876250/tomcat-cache-control) or put a reverse proxy to manipulate the headers (eg: apache, nginx, etc) – Federico Sierra Mar 08 '17 at 19:19
  • 2
    @FedericoSierra not a big deal for intermediate-to-senior level developers to add this functionality manually, but it's quite strange that in 2017 Tomcat doesn't provide this functionality out-of-the-box – Yuriy Nakonechnyy Sep 12 '17 at 15:23
  • 1
    @Yura Yes, I agree with you. – Federico Sierra Sep 12 '17 at 16:03

1 Answers1

0

In order to set 'Cache-Control: no-cache' you will have to create a Custom filter, the ExpiresFilter is used to add 'Expires' and 'Cache-Control: max-age=' headers to HTTP response according to its 'Content-Type' and will not help in setting 'no-cache'

Follow the below steps

  1. A custom filter should be written in order to set the headers, Package the filter inside a .jar

  2. Put the .jar into $CATALINA_HOME/lib

  3. Verify there are no 'ClassNotFoundException' once you add the jar in $CATALINA_HOME/lib


    package nocache;
    
    import javax.servlet.Filter;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest;
    
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.ServletException;
    import java.io.IOException;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    
    public class CacheFilter implements Filter { 
    
        @Override 
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, 
                             FilterChain filterChain) throws IOException, ServletException { 
    
            HttpServletResponse response = (HttpServletResponse) servletResponse; 
            response.setHeader("Cache-Control", "no-cache"); 
            filterChain.doFilter(servletRequest, response); 
        } 
    }

  1. Add the below content to the $CATALINA_HOME/conf/web.xml file

Sample web.xml deployment descriptor snippet


  <filter>
        <filter-name>CacheFilter</filter-name>
        <filter-class>nocache.CacheFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CacheFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

  1. Access the application and checkout the response headers.