No cache filter

When displaying pages in a web application, they are normally dynamic, rendering data from a database. You need to setup the web page so that it will not be cached.

Here's the code for the filter:

package com.---.filter;


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class NoCacheFilter implements Filter

   public void destroy()

   public void doFilter(ServletRequest req, ServletResponse response, FilterChain chain) throws IOException,
      // Set the headers up so that the browser will not cache requests.
      HttpServletResponse resp = (HttpServletResponse)response;
      resp.setHeader("Expires", "Friday, 1 Jan 2010 12:00:00 GMT");
      resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
      resp.addHeader("Cache-Control", "post-check=0, pre-check=0");
      resp.setHeader("Pragma", "no-cache");
      chain.doFilter(req, resp);

   public void init(FilterConfig arg0) throws ServletException

Add this to web.xml:

<!-- Filter to stop the browser caching. -->