First look at where to start from where the garbled, as long as the unified code, there will be no garbled, the following to Uft-8 (personally think the best) as an example, detailed description:
1, if the garbled is from the JSP page appears, JSP head page plus:
<%@ page language= "java" pageencoding= "UTF-8"%>
Add a label to the head tag.
2, if the garbled is in the servlet appears, there are two ways:
One is in each servlet doget and Dopost method head Plus
Request.setcharacterencoding ("Utf-8″");
The second most insurance, once and for all, is dedicated to write a filter class, also known as internationalization, class name Setcharacterencodingfilter content as follows
Copy Code code as follows:
Package com.sharep.filter;//Package 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:
Copy Code code as follows:
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>com.young.filter.setcharacterencodingfilter</filter-class>//Note that this is the class name and 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>
It's done.
3, if there are garbled, is the problem of MySQL database
1 to ensure that the database when the database code selected 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 solve:
One is to specify the encoding in the code that connects the database:
Copy Code code as follows:
String url = "JDBC:MYSQL://LOCALHOST:3306/TEST2?AUTORECONNECT=TRUE&USEUNICODE=TRUE&CHARACTERENCODING=GBK &mysqlEncoding=utf8″;
If it's still not working, it's
Copy Code code as follows:
Show variables like ' collation_% ';
This command to view the default character set, if not Utf-8 My.ini (Windows) or MY.CNF (Linux) to modify the appropriate encoding to UTF8 after the restart of the MySQL server is OK