Using Filter to Disable Browser Caching in Java Web

Posted by sarahk on Mon, 22 Jul 2019 07:43:30 +0200

What is Browser Cache

The browser stores the recently requested documents on the user's disk. When the visitor requests the page again, the browser can display the documents from the local disk, which is the browser cache. Why use browser caching? For browser caching, I believe everyone is familiar with it, because we often use it, but why do we use browser caching? As users, using caching allows us to open a page that we have visited faster. As the administrator of web sites, browser caching can reduce server overhead to a certain extent, while avoiding the waste of bandwidth caused by repeated transmission of the same content. How does browser cache come into being? When the browser requests a certain URL, the server will return the corresponding content according to the request. At this time, the browser will store the content, thus creating the browser cache. The generation of browser cache depends on both browser and web server. There must be communication between them. This communication mechanism is called "cache negotiation".

code implementation

Implementation Class of Custom Filter

package bzu.cn.filter;

import java.io.IOException;

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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HttpFilter implements Filter{
	private FilterConfig filterConfig;
	private FilterConfig getFilterConfig() {
		return this.filterConfig;
	}
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		doFilter((HttpServletRequest)request,(HttpServletResponse)response,chain);
		
	}
	
	//This method is for subclass rewriting
	protected void doFilter(HttpServletRequest req,HttpServletResponse resp, FilterChain chain) {
		
	}
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig= filterConfig;
		       
		init();
		
	}
	
	//This method is for subclass rewriting
	protected void init() {
		
	}
}

Code to disable browser caching

package bzu.cn.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//This is the ability to disable browser caching
public class FilterTest2 extends HttpFilter {
	@Override
	protected void doFilter(HttpServletRequest req, HttpServletResponse resp, FilterChain chain) {
		resp.setHeader("Cache-control","no-cache");
		resp.setHeader("Pragma", "no-cache");
		resp.setDateHeader("Expires", -1);
		System.out.println("Disable browser caching");
		try {
			chain.doFilter(req, resp);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	protected void init() {
		
	}
	
}

Configuration of web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>filterdemo</display-name>
  	<filter>
  		<filter-name>filterTest</filter-name>
  		<filter-class>bzu.cn.filter.FilterTest</filter-class>
  	</filter>
  	<filter-mapping>
  		<filter-name>filterTest</filter-name>
  		<url-pattern>/index.jsp</url-pattern>
  	</filter-mapping>
  	<filter>
  		<filter-name>filterTest2</filter-name>
  		<filter-class>bzu.cn.filter.FilterTest2</filter-class>
  	</filter>
  	<filter-mapping>
  		<filter-name>filterTest2</filter-name>
  		<url-pattern>/*</url-pattern>
  	</filter-mapping>
  	
</web-app>

Topics: xml JavaEE Java Web Server