In the Web page. Each time the client visits the server. There are parts that do not require repeated requests. such as some pictures, videos, etc. there is no need to request each time, this will allow the server to increase the workload. In order to prevent this. We use filters to set the client to be both cached.
Article: Click Open link
HTTP1.1 Enable Cache-control to control the caching or not of the page. Here are some of the frequently used references:
- No-cache, neither the browser nor the cache server should cache page information;
- Public Both the browser and the cache server are able to cache page information.
- No-store. The request and response information should not be stored in the other's disk system;
- Must-revalidate, for each request from the client, the proxy server must want the server to verify that the cache is outdated;
Last-modified only the last generation time of the page, GMT format;
Expires over time limit. GMT format, which means that the browser or cache server must obtain new page information from the real server after that point in time.
The above two values in the JSP set the value to the character type GMT format, cannot take effect, sets the long type to take effect;
The following are the filter codes that are set not to be cached:
<span style= "FONT-SIZE:24PX;" >package cn.hncu.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 Cachefilter implements Filter {@Overridepublic void Destroy () {} </span>
<span style= "FONT-SIZE:24PX;" > @Overridepublic void DoFilter (ServletRequest request, Servletresponse Response,filterchain chain) throws IOException, Servletexception {//filters have very many effects. This is used to set the client whether to set the cache problem, we have here with Response notification client settings do not cache Chain.dofilter (request, response); HttpServletResponse res= (HttpServletResponse) response;res.setheader ("Expries", "1"); Res.setheader ("Pragma", " No-cache "), Res.setheader (" Cache-control "," No-cache ");/* The above can be set without caching, but can also be set to a comprehensive point://Do not agree to the browser side or cache server cache the current page information./* Response.setheader ("Pragma", "No-cache"); Response.setdateheader ("Expires", "1"); Response.AddHeader ("Cache-control", "No-cache");//browser and cache server should not cache page information Response.AddHeader ("Cache-control", "N O-store ");//Request and response information should not be stored on the other side of the disk Response.AddHeader (" Cache-control "," must-revalidate "); *///every request to the client, The proxy server must want the server to verify that the cache is stale. } @Overridepublic void init (Filterconfig arg0) throws servletexception {}}</span><span style= "font-size:18px;" ></span>
Here is the filter file that sets the cache
Package Cn.hncu.filter;import Java.io.ioexception;import Java.util.date;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 CacheFilter2 implements Filter {@Overridepublic void Destroy () {} @Override
public void DoFilter (ServletRequest request, Servletresponse Response,filterchain Chain) throws IOException, Servletexception {//filter has a lot of effect. This is used to set the client whether to set the cache problem, we are here to use Response Notification Client set cache//Here set the cache for 1 days, in order to enable the picture, video class resources (in the filter configuration corresponding to the interception path) require customer browser cache 1 days)// The Intercept path is configured in Web. Chain.dofilter (request, response);//requests are made and returned from here. Return to intercept HttpServletResponse res= (httpservletresponse) response;date D =new Date (); Long time=d.gettime () +60*60*24;//res.setheader ("Expries", "" "+time);//This sets the cache one day Res.setdateheader (" Expries ", Time) ;//As in the previous sentence/* * Date date = new Date (); Response.setdateheader ("Last-modified", Date.gettime ()); Last-modified: The last generation time of the page Response.setdateheader ("Expires", Date.gettime () +60*60*24); Expires: Obsolete term value response.setheader ("Cache-control", "public"); Cache-control to control the page cache or not, public: Browser and cache server can cache page information; Response.setheader ("Pragma", "Pragma"); Pragma: Sets whether the page is cached, is cached for Pragma, and No-cache does not cache */}
@Overridepublic void init (Filterconfig arg0) throws servletexception {}}
The filter file above takes effect and needs to be configured in Web. XML, filtered according to the configured path.
Java EE--------Filter settings cache (filter)