Must-revalidate: for each client request, the proxy server must want the server to verify whether the cache is out of date;Last-Modified: only the Last generation time of the page, in GMT format;
Expires period limit, in GMT format, indicates that the browser or cache server must obtain new page information from the Real Server after this time point;
The above two values are in the GMT format of the simplified type in JSP, and cannot take effect. The long type is only valid;
The following is the filter code for setting non-Cache:
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() {}
@ Overridepublic void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {// the filter has many functions, which is used to set whether the client sets the cache, we use response to notify the client to set a non-Cache chain. doFilter (request, response); HttpServletResponse res = (HttpServletResponse) response; res. setHeader ("expries", "-1"); res. setHeader ("pragma", "no-cache"); res. setHeader ("cache-control", "no-cache");/* You can set No cache, but you can also set a comprehensive one: // the browser or cache server is not allowed to cache the current page information. /* Response. setHeader ("Pragma", "no-cache"); response. setDateHeader ("Expires", "-1"); response. addHeader ("Cache-Control", "no-cache"); // the browser and Cache server should not cache page information response. addHeader ("Cache-Control", "no-store"); // the request and response information should not be stored in the recipient's disk response. addHeader ("Cache-Control", "must-revalidate"); * // the proxy server must verify whether the Cache is out of date for each request from the client ;} @ Overridepublic void init (FilterConfig arg0) throws ServletException {}}
The following is a 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, which is used to set whether the client sets the cache, we use response to notify the client to set cache // here we set cache for 1 day to enable image and video resources (configure the corresponding interception path in the filter) the client browser must be cached for one day) // The interception path is on the web. configure chain in xml. doFilter (request, response); // the first line of the request, the 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); // set the cache for one day res. setDateHeader ("expries", time); // same as the preceding statement/** Date date = new Date (); response. setDateHeader ("Last-Modified", date. getTime (); // Last-Modified: The Last page generation time (response. setDateHeader ("Expires", date. getTime () + 60*60*24); // Expires: past limit response. setHeader ("Cache-Control", "public"); // Cache-Control controls whether the page is cached or not. public: both the browser and the Cache server can Cache page information; response. setHeader ("Pragma", "Pragma"); // Pragma: sets whether the page is cached. If it is set to Pragma, the page is cached. If it is no-cache, the page is not cached */}
@Overridepublic void init(FilterConfig arg0) throws ServletException {}}
The above filter file must be configured in web. xml and filtered Based on the configured path.