HTTP Caching Header Aware Servlet Filter

On the project I’m working on, we’re desperately trying to improve performance. One of the approaches taken by my coworkers was to add the SimplePageCachingFilter from Ehcache, so that Ehcache can serve frequently hit pages that aren’t completely dynamic. However, it occurred to me that the SimplePageCachingFilter can be improved by adding support for the HTTP caching headers (namely, ETags, Expires, Last-Modified, and If-Modified-Since). Adding these headers will do two important things:

  1. Allow Apache’s mod_cache to cache Tomcat served pages, so that requests to these pages never even hit Tomcat, which should massively improve performance
  2. Allow browsers to accurately cache, so visitors don’t need to re-request pages after the first visit

Implementing these headers wasn’t terribly difficult – just tedious in that I had to read the relevant HTTP specification.

I sincerely hope that Ehcache picks up this class and adds it to the next version – I imagine that many applications could benefit from this class!

Here’s my class:

 *  Copyright 2009 Craig Andrews
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import net.sf.ehcache.constructs.web.AlreadyGzippedException;
import net.sf.ehcache.constructs.web.PageInfo;
import net.sf.ehcache.constructs.web.ResponseHeadersNotModifiableException;
import net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter;
 * Filter than extends {@link SimplePageCachingFilter}, adding support for
 * the HTTP cache headers (ETag, Last-Modified, Expires, and If-None-Match.
public class HttpCachingHeadersPageCachingFilter extends
		SimplePageCachingFilter {
	private static final SimpleDateFormat httpDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
	public synchronized static String getHttpDate(Date date){
		return httpDateFormat.format(date);
	public synchronized static Date getDateFromHttpDate(String date) throws ParseException{
		return httpDateFormat.parse(date);
	protected PageInfo buildPage(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws AlreadyGzippedException, Exception {
		PageInfo pageInfo = super.buildPage(request, response, chain);
			//add expires and last-modified headers
			Date now = new Date();
			List<String[]> headers = pageInfo.getHeaders();
			long ttlSeconds = getTimeToLive();
			headers.add(new String[]{"Last-Modified", getHttpDate(now)});
			headers.add(new String[]{"Expires", getHttpDate(new Date(now.getTime() + ttlSeconds*1000))});
			headers.add(new String[]{"Cache-Control","max-age=" + ttlSeconds});
			headers.add(new String[]{"ETag", "\"" + Integer.toHexString(java.util.Arrays.hashCode(pageInfo.getUngzippedBody())) + "\""});
		return pageInfo;
	protected void writeResponse(HttpServletRequest request, HttpServletResponse response, PageInfo pageInfo) throws IOException, DataFormatException, ResponseHeadersNotModifiableException {
		final Collection headers = pageInfo.getHeaders();
        final int header = 0;
        final int value = 1;
		for (Iterator iterator = headers.iterator(); iterator.hasNext();) {
            final String[] headerPair = (String[]);
            		// use the same date we sent when we created the ETag the first time through
            		response.setHeader("Last-Modified", request.getHeader("If-Modified-Since"));
				try {
					String requestIfModifiedSince = request.getHeader("If-Modified-Since");
						Date requestDate = getDateFromHttpDate(requestIfModifiedSince);
		            	Date pageInfoDate = getDateFromHttpDate(headerPair[value]);
		            		response.setHeader("Last-Modified", request.getHeader("If-Modified-Since"));
				} catch (ParseException e) {
					//just ignore this error
		super.writeResponse(request, response, pageInfo);
	/** Get the time to live for a page, in seconds
	 * @return time to live in seconds
	protected long getTimeToLive(){
			return -1;
				return 60*60*24*365; //one year, in seconds
				return blockingCache.getTimeToLiveSeconds();

CC BY-SA 4.0 HTTP Caching Header Aware Servlet Filter by Craig Andrews is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

8 thoughts on “HTTP Caching Header Aware Servlet Filter

  1. Hi Craig, how do you suggest map this filter in web.xml? just for pages that i want or leave for all pages in my web application?

  2. Assuming you’ve stuck to good HTTP design (ex GETs don’t modify data), you can use this filter for all pages, so everything will benefit from the additional headers and caching.

  3. Thanks for this filter.
    I think there might be a concurrency bug : SimpleDateFormat is not a thread safe class, getHttpDate() and getDateFromHttpDate() are synchronized but could be called by two different threads at the same time…

  4. Pingback:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.