Chinese garbled Filter

Source: Internet
Author: User

First, create a class:

 

Public class encodingfilter implements filter {<br/> // encoding format <br/> private string encoding = NULL; <br/> Public void dofilter (servletrequest request, servletresponse response, <br/> filterchain chain) throws ioexception, servletexception {<br/> httpservletrequest httprequest = (httpservletrequest) request; <br/> If (httprequest. getmethod (). equals ("get") {<br/> request = new request (httprequest); <br/>} <Br/> request. setcharacterencoding (this. encoding); <br/> chain. dofilter (request, response); <br/>}< br/>/** <br/> * init implements the javax interface. servlet. filter. init <br/> * Indicates obtaining the web. the value of the parameter "encoding" in XML, both UTF-8 <br/> * <param-Name> encoding </param-Name> <param-value> UTF-8 </param-value> <br/> */<br/> Public void Init (filterconfig FC) throws servletexception {<br/> This. encoding = FC. getinitparameter ("encoding"); <br />}< Br/> Public void destroy () {<br/> This. encoding = NULL; <br/>}< br/>/** <br/> * extended httpservletrequestwrapper? <Br/> */<br/> class request extends httpservletrequestwrapper {<br/> Public request (httpservletrequest request) {<br/> super (request ); <br/>}< br/>/** <br/> * convert the internal code of the data read from the form, convert ISO characters to GBK <br/> */<br/> Public String toChina (string input) {<br/> try {<br/> byte [] bytes = input. getbytes ("ISO8859-1"); <br/> return new string (bytes, encoding); <br/>} catch (exception ex) {<br/>}< br/> return NULL; <br/>} <Br/> private httpservletrequest gethttpservletrequest () {<br/> return (httpservletrequest) super. getrequest (); <br/>}< br/>/** <br/> * read the parameter list, corrected the Chinese problem <br/> * override javax. getparametervalues: intercepts JSP-passed parameters and converts them to Chinese using toChina (). <br/> */<br/> Public String [] getparametervalues (string name) {<br/> string values [] = gethttpservletrequest (). getparametervalues (name); <br/> If (values! = NULL) {<br/> for (INT I = 0; I <values. length; I ++) {<br/> values [I] = toChina (Values [I]); <br/>}< br/> return values; <br/>}< br/>} 

Configure in Web. xml

 

<! -- Character filter configuration --> <br/> <filter-Name> encodingfilter </filter-Name> <br/> <filter-class> COM. JWC. util. filter. encodingfilter </filter-class> <br/> <init-param> <br/> <param-Name> encoding </param-Name> <br/> <param-Value> UTF-8 </param-value> <br/> </init-param> <br/> </filter> 

 

<Filter-mapping> <br/> <filter-Name> encodingfilter </filter-Name> <br/> <URL-pattern>/* </url-pattern> <br /> </filter-mapping> <br/> <filter-mapping> 

 

 

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.