Filter prevents browsers from caching dynamic resources, such as JSP Resources-Learning Notes

Source: Internet
Author: User

When accessing a Web resource, there is no corresponding result, it is possible that the filter does not release resources
Static and dynamic resources are handled differently by the code as follows: Does the ====NO2 segment end with a JSP

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;//prevents browsers from caching dynamic resources, such as JSP resources public class FilterDemo5 implements Filter {//idea privat    e filterconfig filterconfig;    public void init (Filterconfig filterconfig) throws servletexception {this.filterconfig = Filterconfig; } public void DoFilter (ServletRequest req, servletresponse res,filterchain chain) throws IOException, Servletexception        {//no HttpServletRequest The parent-child interface to request = (HttpServletRequest) req;        HttpServletResponse response = (httpservletresponse) res;        NO1 gets the URI of the resource accessed by the client, in the form/day04/login.jsp//http:string uri = Request.getrequesturi (); Static resources and dynamic resources for different cache processing, the code is as follows://no2 sentence is the end of the JSP, that is, dynamic resources IF (uri!=null && uri.endswith ("jsp")) {//NO3 If it is a dynamic resource, set three response headers to notify the browser not to cache Response.setheader ("expire            S ","-1 ");            Response.setheader ("Cache-control", "No-cache");        Response.setheader ("Pragma", "no-cache"); }else if (uri!=null && uri.endswith ("html")) {//no4 If it is a static resource, cache a certain amount of time String strhtml = Filtercon            Fig.getinitparameter ("HTML");            Long time = System.currenttimemillis () +integer.parseint (strhtml) *1000;            Time is the millisecond value response.setdateheader ("Expires", time);            Response.setheader ("Cache-control", time/1000+ "");        Response.setheader ("Pragma", time/1000+ "");    }//no5 Release Resources chain.dofilter (Request,response); } public void Destroy () {}}

Filter prevents browsers from caching dynamic resources, such as JSP resources-Learning notes

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.