2

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,634
  • 7
  • 33
  • 61
  • 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
  • 1
    @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

0 Answers0