Prevent Browser From Caching Resources Using Servlet Filter


In this tutorial I am going to tell you how you can prevent cache in browser using Servlet in web applications. Sometimes you may need to clear the browser cache or prevent the browser from caching resources so that users always get the latest output from the server. Disabling caching of web pages ensures resources are coming every time from the server instead of the cached version.

In every browser, you’ll find an option to disable caching or even you can clear the cached resources but this type of caching is client side caching and generally it is not good idea to rely on whether users will disable cache or clear cache in the browsers. Therefore you may want to ensure that every time a page is requested will bring the updated content from the server.

I assume that you have Java, JSP and Servlet based application where you want to get updated or recent content on your web pages.

Here is an example on how to prevent browser from caching resources.


Java, JSP, Servlet

Build File

The following pom.xml file can be used for the maven based application:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">





HTTP Header Constants

You need to define some constants which are mainly found in HTTP headers of the request.

public enum HTTPCacheHeader {
    private String name;
    private HTTPCacheHeader(String name) { = name;
    public String getName() {

The above enum constants are used for http headers. For more information on enum please read how to create enum in Java.

Servlet Filter

You need to create a Servlet filter that will actually prevent browser caching for each request coming from the client.

This servlet filter actually sets some cache control headers so that browser will not cache resources.

package com.roytuts.servlet.nocache.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 NoCacheFilter() {

	public void destroy() {

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletResponse httpServletResp = (HttpServletResponse) response;
		// set cache directives
		httpServletResp.setHeader(HTTPCacheHeader.CACHE_CONTROL.getName(), "no-cache, no-store, must-revalidate"); // HTTP
																													// 1.1
		httpServletResp.setHeader(HTTPCacheHeader.PRAGMA.getName(), "no-cache"); // HTTP 1.0
		httpServletResp.setDateHeader(HTTPCacheHeader.EXPIRES.getName(), 0L); // Proxies

		// pass the request along the filter chain
		chain.doFilter(request, response);

	public void init(FilterConfig fConfig) throws ServletException {


So the above filter adds http header directives for each web page and prevents caching. You are not actually clearing the cache rather preventing browser from caching resources.

Deployment Descriptor – web.xml

Now in order to let web application work with the servlet you need to make corresponding entry of servlet filter into deployment descriptor – web.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<!-- <web-app xmlns:xsi="" xmlns="" 
	id="WebApp_ID" version="3.0"> -->

<web-app version="4.0"

	<display-name>No Cache Filter in Web App</display-name>





I have put entry of the servlet filter into web.xml file for each requested or forwarded jsp page.

Source Code


Leave a Reply

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