In Java Web projects, the solution to the Chinese garbled method is summarized as follows
In the first case: Call JSP page Chinese display garbled
problem Description: Call JSP page through the browser, the Chinese content displayed in the browser is garbled.
Solution: First of all, confirm that the JSP in the editor to save the contents of the file, the use of UTF-8 encoding format, and then at the beginning of the JSP page add <%@ pageencoding= "Utf-8"%> can solve this Chinese garbled problem
In the second case: Call servlet page display garbled
Problem Description: The browser calls servlet,servlet the content displayed in the browser appears garbled.
Workaround: Before the servlet uses the Response object output, the Response.setcontenttype ("Text/html;charset=utf-8") is executed first, and the encoding for the output is utf-8.
In the third case: Post form pass parameter garbled
Problem Description: The parameters received in the received JSP or servlet are garbled in Chinese when the parameters are submitted to the corresponding JSP or servlet through the jsp,html, or the form elements in the servlet.
For example:
Submit the JSP code as follows:
<%@ page language= "java" pageencoding= "Utf-8"%>
The JSP code that receives the parameters is as follows:
<% @ Page language= "java" pageencoding= "Utf-8"%>
Workaround: Before receiving post submitted parameters, use Request.setcharacterencoding ("Utf-8") to set the content format of the receive parameters to Utf-8 encoding. See the Insert content in the Receive form. Of course, this garbled problem is best to use the Chinese filter method best.
The fourth situation: URI method passing parameter garbled
Problem Description: Use the form to pass parameters, you can post or get way, do not apply the form, you can also use the link to pass parameters, this way of passing parameters is essentially get way pass parameters, this way the parameters passed in Chinese may also appear garbled.
Such as:
Copy Code code as follows:
<a href= "/webproject/display.jsp?username= John &password=123" > Display user name and password </a>
Workaround: The problem essentially is the default encoding of the parameter content passed by get way ask iso8859-1, and using request.setcharacterencoding ("Utf-8") can not solve the problem, to solve this problem, Modify the configuration file for the Tomcat server. Modify line 43rd of the Conf/server.xml file in the Tomcat directory:
Before you modify the contents:
<connector port= "8080" protocol= "http/1.1" "maxthreads=" connectiontimeout= "200000"
8443 "/>
Modified content:
<connector port= "8080" protocol= "http/1.1" "maxthreads=" connectiontimeout= "200000"
8443 "uriencoding=" Utf-8 "/>
This will solve this type of garbled problem.
The fifth situation:
problem Description: In the use of some class libraries or frameworks, in order to achieve the internationalization of the content of the page, you need to write the corresponding properties file. and properties file in the Chinese content in the display of the time will also appear garbled.
Workaround: This garbled problem can be solved by the Native2ascii tool in the JDK. Use the following command:
Copy Code code as follows:
Native2ascii-encoding Utf-8 display.properties display_zh_cn.properties
The reason for the garbled problem is because the Java compiler can only handle Latin-1 or Unicode-encoded character files.
The above is the entire content of this article, I hope to help you learn.