JAVAEE -------- Filter set whether to cache (Filter)

Source: Internet
Author: User

JAVAEE -------- Filter set whether to cache (Filter)

On a webpage, some requests are not required for each client to access the server, such as images and videos. This increases the workload of the server. To prevent this, we use a filter to set that all clients are cached.

Enable Cache-Control in HTTP1.1 to Control the page Cache or not. Here we will introduce several common parameters:

  • No-cache, browser and cache server should not cache page information;
  • Public, the browser and cache server can cache page information;
  • No-store, request and response information should not be stored in the disk system of the other party;
  • 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.

     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.