In the Web page, every time the client Access server, there is no need to repeat the request, such as some pictures, videos, etc. there is no need to request each time, this will let the server increase the workload. To prevent this, we use filters to set the client to be both cached.
Reference article: Click to open a link
HTTP1.1 Enable Cache-control to control the caching of pages or not, here are a few common parameters:
- No-cache, neither the browser nor the cache server should cache the page information;
- Public, both the browser and the cache server can cache page information;
- No-store, request and response information should not be stored in the other's disk system;
- Must-revalidate, for each request of 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 period limit, GMT format, refers to the browser or cache server at that point in time must be from the real server to obtain new page information;
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 is the filter code that is 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 {//filter has many functions, this is used to set the client whether to set the cache problem, we use the Response notification client setting does 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://Does not allow the browser side or cache server to 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", "no-s Tore ");//Request and response information should not be stored on the other side of the disk Response.AddHeader (" Cache-control "," Must-revalidate "), *///on each request of the client, agent service The server must want to verify that the cache is outdated;} @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 many functions, this is used to set the client whether to set the cache problem, we use the response to notify the client setting cache//Here set the cache for 1 days, In order to enable the image, video class resources (configure the appropriate interception path in the filter) requires the client browser cache 1 days)//intercept path in Web. XML configuration chain.dofilter (request, response);//Request First, return will also be 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 and filtered according to the configured path.
Java EE--------Filter settings cache (filter)