Reprint: http://www.jb51.net/article/49253.htm
First of all, it is from where the beginning of the garbled, as long as the unified code, there will be no garbled, the following uft-8 (personally think the best) as an example, detailed description:
1, if garbled is from the JSP page appears, JSP header page plus:
<%@ page language= "java" pageencoding= "UTF-8"%>
Add a label to the head tag.
2. If the garbled characters appear in the servlet, there are two methods:
One is to add the Doget and Dopost method headers in each servlet.
Request.setcharacterencoding ("Utf-8″");
The second most insurance, once and for all, is dedicated to write a filter class, also known as internationalization, the class named Setcharacterencodingfilter content as follows
The code is as follows:
Package com.sharep.filter;//Pack Name
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 Setcharacterencodingfilter implements Filter
{
protected String encoding = NULL;
protected Filterconfig filterconfig = null;
Protected Boolean ignore = true;
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
This.ignore = false;
}
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 Destroy ()
{
this.encoding = null;
This.filterconfig = null;
}
Protected String selectencoding (ServletRequest request)
{
return (this.encoding);
}
}
Then add the following code to the Web-inf Web. xml:
The code is as follows:
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>com.young.filter.setcharacterencodingfilter</filter-class>//Note that this is the class name, with the full package name
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
So it's done.
3, if there is garbled, is the MySQL database problem
1) Ensure that the database is established when the database encoding is utf-8, preferably in each table also specify the encoding format, mysql default is Latin1
2) If the MySQL version is more than 4.x, the database is still garbled, there are the following two ways to resolve:
One is to specify the encoding in the code that connects the database:
The code is as follows: String URL = "jdbc:mysql://localhost:3306/test2?autoreconnect=true&useunicode=true&characterencoding =gbk&mysqlencoding=utf8″;
If it doesn't work, it's just a
The code is as follows: Show variables like ' collation_% ';
This command to view the default character set, if it is not Utf-8, My.ini (Windows) or MY.CNF (Linux) to change the corresponding code to UTF8 after the restart of the MySQL server is OK
Workarounds for JSP and servlet operation MySQL Chinese garbled problem