Java EE filter filter settings cache instance Details _java

Source: Internet
Author: User
Tags time limit browser cache

In the Web page, every time the client Access server, there are some parts do not need to repeat the request, such as some pictures, video and so there is no need to request each time, this will allow the server to increase the workload. To prevent this, we use filters to set the client to be both cached.

Page caching and not caching settings and meta functions in HTML pages

HTTP1.1 to enable Cache-control to control the caching of the page, here are some common parameters:

No-cache, browsers and caching servers should not cache page information;

Public, browser and caching server can cache page information;

No-store, requests 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 obsolete;

Last-modified only the last generation time of the page, GMT format;

Expires time limit, GMT format, refers to the browser or cache server after that point must be from the real server to obtain new page information;

The above two values in the JSP set the value of the GMT format of the character type, cannot take effect, set long to take effect;

The following is a filter code that does not cache:

<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-siz e:24px; " > @Override public void Dofilter (ServletRequest request, servletresponse response, Filterchain chain) throws Ioexce 
Ption, Servletexception {//filter has many functions, this is used to set the client whether to set the cache, we use the response to notify the 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 set a comprehensive point://Do not allow browser-side or cache server cache Current page letterInterest. 
/* 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 ")//Request and response information should not be stored on each other's disk Response.AddHeader (" Cache-control "," Must-revalidate "), *///on each request of the client, The proxy server must want the server to verify that the cache is obsolete;} @Override public void init (Filterconfig arg0) throws servletexception {}}</span><sp An 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 (serv Letrequest request, servletresponse response, Filterchain chain) throws IOException, Servletexception {//filter has many functions, this 
To set the client whether to set the caching problem, we use response to notify the client to set the cache//here set cache 1 days, to achieve the picture, video class resources (in the filter to configure the appropriate interception path) to require client browser cache 1 days)//intercept path in Web.xml configuration 
Chain.dofilter (request, response);//First, return also 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);//Same as the preceding 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 caching of pages, public: browsers and caching servers can cache page information; Response.setheader ("Pragma", "Pragma");  
Pragma: Set whether the page is cached, Pragma is cached, No-cache does not cache/} @Override public void init (Filterconfig arg0) throws Servletexception { }

Note: The filter file above should be configured in Web.xml and filtered according to the configured path.

The above is a small set to introduce Java EE filter filter settings are cached examples of relevant knowledge, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.