30

How do you add an Expires or a Cache-Control header in JSP? I want to add a far-future expiration date in an include page for my static components such as images, CSS and JavaScript files.

BartoszKP
  • 32,105
  • 13
  • 92
  • 123
Sam
  • 1,005
  • 2
  • 9
  • 8

4 Answers4

72

To disable browser cache for JSP pages, create a Filter which is mapped on an url-pattern of *.jsp and does basically the following in the doFilter() method:

HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1
httpResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0
httpResponse.setDateHeader("Expires", 0); // Proxies.

This way you don't need to copypaste this over all JSP pages and clutter them with scriptlets.

To enable browser cache for static components like CSS and JS, put them all in a common folder like /static or /resources and create a Filter which is mapped on an url-pattern of /static/* or /resources/* and does basically the following in the doFilter() method:

httpResponse.setDateHeader("Expires", System.currentTimeMillis() + 604800000L); // 1 week in future.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • 2
    thanks it helped me much. But isn't there an error on the seconde line? Imho it should be: httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); – Christian Apr 03 '12 at 14:36
  • 1
    I found this article from a book: http://www.onjava.com/pub/a/onjava/excerpt/jebp_3/index2.html, they set some extra headers for IE, `// Set IE extended HTTP/1.1 no-cache headers (use addHeader). res.addHeader("Cache-Control", "post-check=0, pre-check=0");` – Christophe Roussy Jul 15 '13 at 14:15
  • @Christophe: that's a myth. That applies only to cacheable resources. You don't need this on a resource which is already not cacheable. You see this additional header often when one made the mistake that a page was unintentionally served as cacheable and then the developer fixed it by making it non-cacheable. However, without those 2 directives it would still stick around in IE until the enduser explicitly clears IE cache themselves. – BalusC Jul 15 '13 at 14:19
  • When I did this nothing was coming over the screen. I have two filters 1> myApp for this url pattern as /* 2> CacheManagerFilter for this url pattern as *.jsp , might be this is the problem ? how can I handle this situation ? – Sadanand May 14 '14 at 08:35
  • Apparently you forgot to continue the filter chain. – BalusC May 14 '14 at 09:51
  • @BalusC setting expires to -1 would be a better advise and never use Expires = 0 to prevent caching. The Expires header is sent by the remote server and passed through to the browser by the Portal Server. Unless the time on all three machines is synchronized, an Expires=0 header can mistakenly return cached content. To solve this problem, set the Expires header to a fixed date that is definitely in the past or -1. – pokemon blue Nov 17 '16 at 11:56
  • @pokemonblue Uhm, code uses `setDateHeader()` not `setHeader()`. – BalusC Nov 17 '16 at 12:12
  • Is there any reason you chose to write your own `Filter` rather than using `Tomcat`'s predefined `Filter`: [`ExpiresFilter`](https://tomcat.apache.org/tomcat-8.5-doc/api/org/apache/catalina/filters/ExpiresFilter.html)? – theyuv Apr 20 '21 at 13:20
  • @theyuv: so that the webapp can run on any other server than Tomcat. Using a Tomcat-specific filter would tight-couple the webapp to be deployable on Tomcat only. This tight-coupling is a bad practice. Use Tomcat built-in filter only as being server operator to fix an existing train wreck webapp without need to modifiy its source code. – BalusC Apr 20 '21 at 13:40
10
<%
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
%>
Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
2
<%
    response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
    response.setHeader("Pragma", "no-cache"); //HTTP 1.0
    response.setDateHeader("Expires", 0); //prevents caching at the proxy server
%>
User1985
  • 65
  • 1
  • 8
1

Servlet containers like Tomcat come with a set of predefined filters. See for example Expires Filter. It may be easier to use existing one than to create your own similar filter.

mp31415
  • 5,743
  • 1
  • 40
  • 32