First of all, from where the beginning of the garbled, as long as the unified encoding, will not appear garbled, the following uft-8 (I think the best) as an example, details:
1. If garbled characters appear on the jsp page, add the following to the jsp header page:
<% @ Page language = "java" pageEncoding = "UTF-8" %>
Add a label to the head label.
2. If garbled characters appear in the servlet, there are two methods:
One is to add the doget and doPost Methods header in each servlet
Request. setCharacterEncoding ("UTF-8 ″);
The second type is the most safe. Once and for all, it is specialized in writing a filter class, also known as internationalization. The class name is SetCharacterEncodingFilter. The content of the class is as follows:
Copy codeThe Code is 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. inclusignorecase ("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 );
}
}
Add the following code to the web. xml file of web-inf:
Copy codeThe Code is as follows:
<Filter>
<Filter-name> SetCharacterEncoding </filter-name>
<Filter-class> com. young. filter. SetCharacterEncodingFilter </filter-class> // note that the class name must be complete.
<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>
This is done.
3. If garbled characters exist, it is a problem with the mysql database.
1) ensure that the database encoding option is UTF-8 when the database is created. It is best to specify the encoding format in each table. The default value of mysql is latin1.
2) If the mysql version is later than 4.x, there are still garbled characters in the database. There are two solutions:
One is to specify the encoding method in the Code connecting to the database:
Copy codeThe Code is as follows: String url = "jdbc: mysql: // localhost: 3306/test2? AutoReconnect = true & useUnicode = true & characterEncoding = gbk & mysqlEncoding = utf8 ″;
If the problem persists, use
Copy codeThe Code is as follows: show variables like 'collation _ % ';
This command is used to check the default character set. If it is not UTF-8, change the encoding in my. ini (windows) or my. cnf (linux) to utf8 and restart the mysql server.