Java Expiry Date header


One of the tricks to creating snappy and fast loading web pages is to set the correct ‘Expiry-date’ header on your web pages and static resources like images, CSS and Javascript files. By setting an expiration date in the future, you can tell the browser to cache the resource locally in the browser cache. This article discusses how to speed up your Java webapp by setting optimal expiration date to leverage browser caching.

An example of expiry date header is as follows:

Expires: Wed, 27 Oct 2010 19:02:24 EDT

The easiest way to set expiry date in Java webapps is via a servlet filter. Servlet filters are Java classes which extend the class javax.servlet.Filter. Servlet filters are an elegant way to intercept each request and do something useful, add a HTTP header in this case.

I wrote a Servlet filter which can be used to set the expiry date on any page in your webapp. You can use this servlet filter to set the expiry date header without writing any Java code in your project. You can also select which pages to set the expiry date and the age of the page via a properties file.

1. Download the code for expiration date filter and add it to your project

Download the file and add it to your project.

2. Add a Servlet Filter to web.xml, and map it to all URLs



3. Create ‘’ file in src/main/resources

Create the following file:


You can set expiry date for all or some paths in your webapp by simply creating entries in this file

The format for each line is:

Resource Path = freshness interval for this resource

<full regex of the resource> = <number><unit>

Valid values for Unit:

  • m for minute
  • h for hour
  • d for day
  • w for week
  • M for month


  1. Order in the properties file is important. The first rule which matches is applied and no more rules are checked.
  2. If no rules match, expiry date is not set.
  3. The path of the resource relative to the application root is matched against the regex. So if you app baseurl is http://foobar/somewebapp, the path for resource http://foobar/somewebapp/images/logo.png will be /images/logo.png.

Example 1:

^/css/.*  = 30d
^/js/.*   = 15d
^.*\.png$ = 2M
  • The first line specifies, all content starting with /css expires in 30 days from current date.
  • The second line specifies, all content starting with /js expires in 15 days from current date.
  • The third line specifies, all content ending with .png expires in 15 days from current date.
  • For those less familiar with regular expression, ^ denotes the beginning of the line and $ denotes the end of line. See full regex reference.

Example 2: For pages which don’t change a lot, you may want to set a smaller expiry date, says 4 hours. This is merely an example, you should examine the nature of your webapp before choosing the age of the page.

^/home/content/ = 4h

Example 3: Depending on how aggressive you want to be, you may want to set a default expiry date for all pages. This might not be suitable in all cases where pages change often and you want the browser to always load the latest page. Don’t use this if you don’t understand it completely.

.* = 1h

How to check Expiry Date header via a browser

Viewing Page Expiry Header in Firebug

Viewing HTTP Headers for in Firebug

There are many tools that let you check HTTP header values. My personal favorite is Firebug, which is a Firefox plugin. Once you have firebug open, go to the “Net” console, and view your page. You should see a GET or POST request as a row on the console. Expand that row, and go to the “Headers” tab. You should see all the headers, including the “Expires” headers if present.

The following screenshot is Firefug’s Net console displaying HTTP headers from Notice that the expiry date is for home page is only 1 minute. This lets the browser cache the page for 1 minute, and re-fetch the page after 1 minute. While this is suitable for where content might change every minute, you should decide the expiry date for your website depending on how often the content changes.

I highly recommend getting Firebug and looking at the expiry date of your webapp. Firebug also shows what gets downloaded each time you request a page and what is loaded from cache. This may help you determine your requirements, for example, if you notice a static resource (javascript file or css file) getting downloaded in each request, you might want to add an expiry date header for that resource.


Setting an expiration date in the future lets user’s browser cache pages locally and results in fast loading pages and increased end user satisfaction. The technique described in this article lets Java webapps very easily set expiration date without writing any Java code. However, keep in mind that setting a long expiration date might not be the right thing to do in many cases, e.g. where freshness of content is important, or there are security implications.


Related posts:

  1. Cache Java webapps with Squid Reverse Proxy
  2. Access to DTDs blocked from Java
  3. Using Google Analytics Event API to track your website performance

Leave a Reply




You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Get Adobe Flash playerPlugin by wordpress themes