javaweb-Filter Study (ii) setting the full-station encoding and setting the page cache

Source: Internet
Author: User

We used to set the servlet request and response encoding needs to be set in each servlet, if the servlet is a lot of trouble, now we can use the filter is very simple to implement this function.
There is also page cache, if our page is static, the picture and content basically rarely change or change, we can tell the client how long the page you cache ~ To achieve the purpose of saving traffic.

Set the full station encoding:

Write a good Filter:CharacterFilter.java first:

 PackageCn.hncu.pubs;ImportJava.io.IOException;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;ImportJavax.servlet.ServletException;ImportJavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse; Public  class characterfilter implements Filter{    PrivateString CharSet;//Get the encoding parameter value for the Web. XML Configuration    @Override     Public void Init(Filterconfig filterconfig)throwsservletexception {charset = Filterconfig.getinitparameter ("CharSet"); }@Override     Public void DoFilter(ServletRequest request, servletresponse response, Filterchain chain)throwsIOException, servletexception {request.setcharacterencoding (charset);        Response.setcharacterencoding (CharSet); Chain.dofilter (request, response);//Release}@Override     Public void Destroy() {    }}

Configure the Web. xml file:

If you do not know where the filter should be written, you can click the Web-app tab in MyEclipse, and then press and hold F2 to display the label's writing position.

<?xml version= "1.0" encoding= "UTF-8"?><web-app version="3.0"xmlns="Http://java.sun.com/xml/ns/javaee"  xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">                 <display-name></display-name>   <filter>    <filter-name>Character</filter-name>    <filter-class>Cn.hncu.pubs.CharacterFilter</filter-class>    <init-param>        <!--Configuring encoding parameters and values --        <param-name>CharSet</param-name>        <param-value>Utf-8</param-value>    </init-param>  </filter>  <filter-mapping>    <filter-name>Character</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></Web-app>
Set Page Caching

Also very simple, we write several response headers in the filter, and then configure the Url-pattern path in the Web. Xml that needs to be cached.

 PackageCn.hncu.pubs;ImportJava.io.IOException;ImportJava.util.Date;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;ImportJavax.servlet.ServletException;ImportJavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse;ImportJavax.servlet.http.HttpServletResponse; Public  class jspcachefilter implements Filter{    @Override     Public void Init(Filterconfig filterconfig)throwsServletexception {}@Override     Public void DoFilter(ServletRequest request, servletresponse response, Filterchain chain)throwsIOException, servletexception {HttpServletResponse resp = (httpservletresponse) response;//Set cache timeDate Date =NewDate ();LongTime = Date.gettime () + +* -* -* -*5;//Cache 5 daysResp.setheader ("Expires",""+time); Resp.setheader ("Pragma",""+time); Resp.setheader ("Cache-control",""+time);//Set 3 to be compatible with different browsers        //With the modified RESP back to the platformChain.dofilter (Request, RESP); }@Override     Public void Destroy() {    }}

Settings do not cache: (This may not work because most browsers have their own cache)
The reason for setting these 3 is that different browsers have different names for their parameters! In order to be compatible with most browsers, we have set up 3 of these.

resp.setHeader("expires","-1");resp.setHeader("pragma""no-cache");resp.setHeader("cache-control""no-cache");

As for the config file of Web. XML, I will not write, you can follow the above write.

Reprint please attach the original blog link:

http://blog.csdn.net/qq_26525215

javaweb-Filter Study (ii) setting the full-station encoding and setting the page cache

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.