A method for processing Chinese garbled characters in the filter filters in Java _java

Source: Internet
Author: User

Attention to the problem: in learning to use Selvert filter to handle Chinese garbled, in the filter configuration initialization with the Utf-8 processing Chinese garbled, and in the submission of the JSP page is used GBK. Although both can come out of Chinese garbled, but it caused the processing of garbled format inconsistency. So there was a compilation error.

Solution: Use Utf-8 or GBK everywhere

Copy Code code as follows:

Filter class
charactorfilter.jsp
Package cn.com.Filter;

Import java.io.IOException;

Import Javax.servlet.Filter;
Import Javax.servlet.FilterChain;
Import Javax.servlet.FilterConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;

public class Charactorfilter implements filter {//Inheritance filter class
Character encoding
String Encoding=null;

public void Dofilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {
if (encoding!=null) {
Set Request character encoding
request.setcharacterencoding (encoding);
Set Response character encoding
Response.setcontenttype ("text/html;charset=" +encoding);
}
Pass to next filter
Chain.dofilter (request, response);

}

public void init (Filterconfig filterconfig) throws Servletexception {
Get initialization parameters
Encoding=filterconfig.getinitparameter ("encoding");

}

public void Destroy () {
TODO auto-generated Method Stub
Encoding=null;
}

}

Xml

Copy Code code as follows:

<filter> <!--Note that this is filter, do not configure as Servlet-->
<filter-name>CharactorFilter</filter-name> <!--filter name-->
<filter-class>cn.com.Filter.CharactorFilter</filter-class> <!--filter Complete class name-->
<init-param> <!--initialization parameters-->
<param-name>encoding</param-name> <!--parameter name-->
<param-value>utf-8</param-value> <!--parameter value-->
</init-param>
</filter>
<filter-mapping> <!--filter mapping-->
<filter-name>CharactorFilter</filter-name><!--Filter Name-->
<url-pattern>/*</url-pattern><!--URL mapping, to deal with garbled--> for all pages
</filter-mapping>

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.