"DRP" Filter Interceptor-Application

Source: Internet
Author: User

Usage

1. Set up the configuration file:

1  PackageCom.bjpowernode.drp.util.filter;2 3 Importjava.io.IOException;4 5 ImportJavax.servlet.Filter;6 ImportJavax.servlet.FilterChain;7 ImportJavax.servlet.FilterConfig;8 Importjavax.servlet.ServletException;9 Importjavax.servlet.ServletRequest;Ten ImportJavax.servlet.ServletResponse; One  A /** - * Uniform processing of character sets using filter -  * @authorYin the  * -  */ -  Public classCharsetencodingfilterImplementsFilter { -  + @Override -      Public voiddestroy () { +  A     } at  - @Override -      Public voidDoFilter (servletrequest request, servletresponse response, -Filterchain chain)throwsIOException, servletexception { -          -         //Set character sets inRequest.setcharacterencoding ("GB18030"); -         //Continue execution to Chain.dofilter (request, response); +     } -  the @Override *      Public voidInit (Filterconfig arg0)throwsservletexception { $     Panax Notoginseng  -     } the  +}

2, configure the Web. XML configuration file

1<web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee"2Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"3Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee4http//java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">5     6<!--filename and file-relative path--7<filter>8<filter-name>CharsetEncodingFilter</filter-name>9<filter-class>com.bjpowernode.drp.util.filter.charsetencodingfilter</filter-class>Ten</filter> One  A<!--file names and blocked pages- -<filter-mapping> -<filter-name>CharsetEncodingFilter</filter-name> the<url-pattern>*.jsp</url-pattern> -</filter-mapping> -      -</web-app>

"Improved"-the character set is obtained by reading the XML file

Web. XML file:

1<web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee"2Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"3Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee4http//java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">5     6<!--filename and file-relative path--7<filter>8<filter-name>CharsetEncodingFilter</filter-name>9<filter-class>com.bjpowernode.drp.util.filter.charsetencodingfilter</filter-class>Ten<init-param> One<param-name>encoding</param-name> A<param-value>GBK</param-value> -</init-param> -</filter> the  -<!--file names and blocked pages- -<filter-mapping> -<filter-name>CharsetEncodingFilter</filter-name> +<url-pattern>*.jsp</url-pattern> -</filter-mapping> +      A</web-app>

Filter file

1  PackageCom.bjpowernode.drp.util.filter;2 3 Importjava.io.IOException;4 5 ImportJavax.servlet.Filter;6 ImportJavax.servlet.FilterChain;7 ImportJavax.servlet.FilterConfig;8 Importjavax.servlet.ServletException;9 Importjavax.servlet.ServletRequest;Ten ImportJavax.servlet.ServletResponse; One  A /** - * Uniform processing of character sets using filter -  * @authorYin the  * -  */ -  Public classCharsetencodingfilterImplementsFilter { -  +     PrivateString endcoding; - @Override +      Public voiddestroy () { A  at     } -  - @Override -      Public voidDoFilter (servletrequest request, servletresponse response, -Filterchain chain)throwsIOException, servletexception { -          in         //Set character sets - request.setcharacterencoding (endcoding); to         //Continue execution + Chain.dofilter (request, response); -     } the  *     //Initialize Method $ @OverridePanax Notoginseng      Public voidInit (Filterconfig filterconfig)throwsservletexception { -          //read the value of the character set in the Web. xml File the          This. endcoding = Filterconfig.getinitparameter ("Encoding"); + System.out.println (endcoding); A  the     } +  -}

Request.getparameter () and Request.getattribute () differencesJavaweb Filters FilterJavaweb Learning Summary (42)--filter (filter) Learning

Summary: Constantly improve their coding efficiency!

"DRP" Filter Interceptor-Application

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.