Recently troubled me a long time problem finally solved, for him I have a headache for a few days, the problem is JSP through the servlet to the database value, query display on the page when there is garbled, the original I have two rows of data in the database with Chinese, the query is not garbled, I debug a bit, found that the JSP and servlet all accept the Chinese character set of the variables are not garbled, I went to the database to view, all the added Chinese characters are question marks, the problem found, I Baidu what reason, someone said to change the Tomcat character set, Accept Chinese characters by changing the character set of the Server.xml file.
Method One:
<connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000" redirectport= "8443"
I tried it or it didn't work out.
Method Two:
If you send data to the database through a servlet that contains Chinese, you can set it in the servlet again
Request.setcharacterencoding ("UTF-8"); Response.setcontenttype ("Text/html;charset=utf-8");
Method Three:
Set the character set of CharSet on the page
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" Utf-8 "%>
Method Four:
Define the encoding in the Web. xml file, and define the encoding as Utf-8 in the Setcharacterencodingfilter class
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 encoding response.setcharacterencoding ("UTF-8") ;//Processing code chain.dofilter (Request, response);//Let filter execute next request } public void Destroy () { } public void Init (Filterconfig arg0) throws servletexception { }
Method Five: Define the code in form form
accept-charset= "Utf-8" onsubmit= "document.charset= ' utf-8 ';"
These methods have been tried, or there is garbled. Helpless, then in the group to consult the great God, there is a big God said, in the MySQL database run the following sentence
Show variables like '%char% ';
After I run the result is
The great God said that I need to change these two,
Then I'll find the My.ini file in the installation directory. The following sentences are given as follows:
Default-character-set=utf8default-storage-engine=innodb
Then restart MySQL, and then the problem is resolved.
QQ number with the group, 293074111 hope that the group can help the students who really love learning Java
The problem of garbled value between Jsp,servlet and database