Java Expiry Date Header
Java Expiry Date Header
Join the DZone community and get the full member experience.Join For Free
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 PageExpiryFilter.java and add it to your project.
2. Add a Servlet Filter to web.xml, and map it to all URLs
3. Create ‘expires.property’ 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
- Order in the properties file is important. The first rule which matches is applied and no more rules are checked.
- If no rules match, expiry date is not set.
- 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.
^/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
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 www.cnn.com. Notice that the expiry date is for www.cnn.com 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 cnn.com where content might change every minute, you should decide the expiry date for your website depending on how often the content changes.
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.
- PageExpiryFilter.java source on Google Code
- Regex reference
- Google’s page optimization guide on Expiry header and Browser Caching
Opinions expressed by DZone contributors are their own.