Garbled characters occur when values are transmitted between JSP, servlet, and database.
A problem that has plagued me for a long time has finally been solved by zookeeper. It has been a headache for me for several days. The problem is that JSP transfers values to the database through servlet, garbled characters appear when the query is displayed on the page. Originally, there were two rows of data with Chinese characters in my database, but there was no garbled code during the query. Let me debug it, I found that no garbled characters were found in all the variables in JSP and servlet that accepted the Chinese character set. I went to the database and checked that all the added Chinese characters were question marks. if the problem was found, why would I go to Baidu, some people say that the tomcat character set is changed by changing the server. xml file character set to accept Chinese characters,
Method 1:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
I tried to solve the problem,
Method 2:
If the data sent to the database through servlet contains Chinese characters, you can set it in the servlet.
request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8");
Method 3:
Set charset character set on the page
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Method 4:
Define the encoding in the web. xml file and define the Encoding As UTF-8 in the SetCharacterEncodingFilter class.
Web. xml:
<filter><filter-name>SetCharacterEncodingFilter</filter-name><filter-class>com.bzu.servlet.SetCharacterEncodingFilter</filter-class></filter><filter-mapping><filter-name>SetCharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
SetCharacterEncodingFilter class:
Public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {request. setCharacterEncoding ("UTF-8"); // process the encoding response. setCharacterEncoding ("UTF-8"); // process the encoding chain. doFilter (request, response); // Let the filter execute the next request} public void destroy () {} public void init (FilterConfig arg0) throws ServletException {}
Method 5: Define encoding in form
accept-charset="utf-8" onsubmit="document.charset='utf-8';"
These methods have been tried and garbled. However, I asked the experts in the Group and said, "run the following statement in the MySQL database ."
show variables like '%char%';
The result after I run it is
I need to change the two of them,
Then I will find the my. ini file in the installation directory and give the following statements in the following format:
default-character-set=utf8default-storage-engine=INNODB
Restart MySQL, and solve the problem.
QQ number of the affiliated group, 293074111 hope the group can help students who really love learning Java
Zookeeper