How filter filter in Java handles Chinese garbled characters

Source: Internet
Author: User

Note: When learning to use Selvert filter to handle Chinese garbled characters, when the filter configuration is initialized with Utf-8 processing Chinese garbled, but in the submitted JSP page used GBK. Although both can come out in Chinese garbled, but it caused the processing garbled format inconsistent. So compile error.

Workaround: Use Utf-8 or GBK everywhere

//Filter Classcharactorfilter.jsp PackageCn.com.Filter;Importjava.io.IOException;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse; Public classCharactorfilterImplementsFilter {//inherit the filter class//character encodingString encoding=NULL;  Public voidDoFilter (servletrequest request, servletresponse response, Filterchain chain)throwsIOException, servletexception {if(encoding!=NULL){        //set the request character encodingrequest.setcharacterencoding (encoding); //Set Response character encodingResponse.setcontenttype ("text/html;charset=" +encoding); }     //Pass to next filterChain.dofilter (request, response); }     Public voidInit (Filterconfig filterconfig)throwsservletexception {//Get initialization ParametersEncoding=filterconfig.getinitparameter ("Encoding"); }     Public voiddestroy () {//TODO auto-generated Method Stubencoding=NULL; }}

Xml

<filter> <!--Note This is filter, do not configure it as servlet-->    <filter-name>CharactorFilter</filter-name> <!--filter Name--   <filter-class>cn.com.filter.charactorfilter</filter-class> <!--filter's full class name--       <init-param> <!--initialization Parameters--           <param-name>encoding</param-name> <!--parameter name---           <param-value>utf-8</param-value> <!--parameter Values--       </init-param>  </filter>  <filter-mapping> <!--filter Map--       <filter-name>CharactorFilter</filter-name><!--Filter Name--         <url-pattern>/*</url-pattern><!--URL mapping, handling all pages garbled--</filter-mapping>

How filter filter in Java handles Chinese garbled characters

Related Article

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.