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