First look at where the garbled characters start to appear. As long as the encoding is unified, there will be no garbled characters. The following takes uft-8 (personally think it is the best) as an example to explain in detail:
1. If The garbled code appears from the jsp page. Add the jsp header page:
<%@ page language="java" pageEncoding="UTF-8" %>
Add the tag in the head tag.
2. If garbled characters appear in the servlet, there are two methods:
One is to add
request.setCharacterEncoding(" to the header of the doget and doPost methods in each servlet UTF-8″);
The second option is the safest, once and for all, it is to write a specially designed filter class, also known as internationalization. The class name is SetCharacterEncodingFilter and the content is as follows
package com.sharep.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 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 in the web of web-inf Add the following code to .xml:
SetCharacterEncoding com.young.filter.SetCharacterEncodingFilter //注意这里是类名,要有完整包名encoding UTF-8 SetCharacterEncoding /*
This is done
3. If there are still garbled characters, it is a problem with the mysql database
1) Ensure that the database is established When the database encoding is selected, it is UTF-8. It is best to specify the encoding format in each table. MySQL default is latin1
2) If the MySQL version is 4.x or above, garbled characters still appear in the database. There are two types: Solution:
One is to specify the encoding method in the code to connect to the database:
String url = “jdbc:mysql://localhost:3306/test2?autoReconnect=true&useUnicode=true&characterEncoding=gbk&mysqlEncoding=utf8″ ;
If it still doesn’t work, use the
show variables like ‘collation_%';
command to check the default character set, if it is not utf If -8, change the corresponding encoding to utf8 in my.ini (windows) or my.cnf (linux) and then restart the mysql server and it will be ok
More solutions to the problem of mysql Chinese garbled characters in jsp and servlet operations For articles related to methods, please pay attention to the PHP Chinese website!