In the web page, when the client accesses the server every time, some of them do not need to request it repeatedly. For example, some pictures, videos, etc., there is no need to request it every time, which will increase the workload of the server. To prevent this, we use filters to set the client to be cached.
Page cache and non-cache settings and the role of meta in html pages
In HTTP 1.1, Cache-Control is enabled to control the cache of pages. Here are a few commonly used parameters:
no-cache, neither browser nor cache server should cache page information;
public, both the browser and the cache server can cache page information;
no-store, the request and response information should not be stored in the other party's disk system;
Must-revalidate, for each request from the client, the proxy server must want the server to verify that the cache is out of date;
Last-Modified only the last generation time of the page, GMT format;
Expires expired limit value, GMT format, means that the browser or cache server must obtain new page information from the real server after this time point;
The above two values are set to character-type GMT format in JSP, and cannot take effect, so the long type is set to take effect;
Here is the filter code that sets uncached:
<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 { @Override public void destroy() { }</span> <span style="font-size:24px;"> @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // Filters have many functions. This is used to set whether the client sets cache. We use response here to notify the client to set up 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 to not cache, but it can also be set to be more comprehensive: //The browser side 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" );//Neither the browser nor the cache server should cache page information response.addHeader( "Cache-Control", "no-store" );//Non of the request and response information should be stored on the other party's disk response.addHeader( "Cache-Control", "must-revalidate" );*//Every request from the client, the proxy server must want the server to verify whether the cache is out of date; } @Override public void init(FilterConfig arg0) throws ServletException { } }</span><span style="font-size:18px;"> </span>Below 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 { @Override public void destroy() { } @Override public void doFilter(ServletRequest request , ServletResponse response, FilterChain chain) throws IOException, ServletException { // Filters have many functions. This is used to set whether the client sets cache. We use response to notify the client to set the cache // Set the cache for 1 day here to enable the resource of the image and video class (configure the corresponding intercept path in the filter) to require the client browser to cache for 1 day) // Intercept path configure chain.doFilter(request, response);//Make the request first, return will also pass here, return to intercept HttpServletResponse res=(HttpServletResponse) response; Date d =new Date(); Long time=d.getTime()+60*60*24; // res.setHeader("expries", ""+time);//This way, set cache for one day res.setDateHeader("expries", time);//Same as the above 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: The expiration limit response.setHeader("Cache-Control", "public"); //Cache-Control controls whether the page is cached, public: Both the browser and the cache server can cache page information; response.setHeader("Pragma", "Pragma"); //Pragma: Set whether the page is cached, if it is Pragma, it is cached, and no-cache is not cached*/ } @Override public void init(FilterConfig arg0) throws ServletException { }Note: The above filter file needs to be configured in web.xml to filter according to the configured path.
The above is the detailed explanation of whether the JAVAEE Filter filter setting is cached instances introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!