Solving method of Java Chinese character garbled problem

Source: Internet
Author: User

Solving method of Java Chinese character garbled problem
The following about the web Effects phttp://www.111cn.net/Web page special effects p.html Target=_blank >jsp Tutorial Chinese characters garbled problem we are doing a filter, and then configure the web, so good to solve the JSP in the encoding problem OH.
*/

public class Characterfilter implements filter {

public void Destroy () {

}

public void Dofilter (ServletRequest request, servletresponse response,
Filterchain filter) throws IOException, Servletexception {

Request.setcharacterencoding ("Utf-8");
Response.setcharacterencoding ("Utf-8");
Filter.dofilter (request, response);
}

public void init (Filterconfig arg0) throws Servletexception {

}

}


%>
and www.111cn.net in the Web configuration.

<filter>
<filter-name>characterfilter</filter-name>
<filter-class>org.shopping.uitl.characterfilter</filter-class>
</filter>


Let's take a look at the official method of dealing with Chinese characters garbled. Mb.111cn.net

<%
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;
/**
* Filter, set text encoding format
*/
public class Setcharacterencodingfilter implements filter {

protected string encoding = NULL;

protected Filterconfig filterconfig = null;

Protected Boolean ignore = true;

public void Destroy () {

this.encoding = null;
This.filterconfig = null;

}

public void Dofilter (ServletRequest request, servletresponse response,
Filterchain chain)
Throws IOException, Servletexception {

if (Ignore | | (request.getcharacterencoding () = null)) {
String encoding = selectencoding (request);
if (encoding!= null)
request.setcharacterencoding (encoding);
}

Chain.dofilter (request, response);

}

    public void init (Filterconfig filterconfig) throws Servletexception {

This.filterconfig = Filterconfig;
this.encoding = Filterconfig.getinitparameter ("encoding");
String value = Filterconfig.getinitparameter ("Ignore");
if (value = = null)
This.ignore = true;
else if (Value.equalsignorecase ("true"))
This.ignore = true;
else if (value.equalsignorecase ("yes"))
This.ignore = true;
Else
This.ignore = false;

}

Protected string selectencoding (ServletRequest request) {

return (this.encoding);

}


}

%>

The configuration of the filter in XML:

XML code
<filter>
<filter-name>setcharacterencodingfilter</filter-name>
<filter-class>
Package name. setcharacterencodingfilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setcharacterencodingfilter</filter-name>
<url-pattern>/*</url-pattern>
</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.