In Java Web project, the method of solving Chinese garbled code is summarized as follows
The first case: Calling the jsp page to display garbled code in Chinese <br />Problem description: Calling the jsp page through the browser, garbled code appears in Chinese content displayed in the browser.
Solution: First, confirm that when this jsp saves the file content in the editor, it uses the encoding format of utf-8, and then add <%@ pageEncoding="utf-8"%> to the beginning of the jsp page to solve this problem of garbled Chinese
The second case: Calling the servlet page shows garbled code
Problem description: When calling servlet through the browser, the content displayed in the servlet appears garbled.
Solution: Before the servlet uses the response object to output, execute response.setContentType("text/html;charset=utf-8"); set the encoding of the output content to utf-8.
The third case: garbled code of the post form
Problem description: When the parameters are submitted to the corresponding jsp or servlet through the form elements in jsp, html, or servlet, the parameters received in the received jsp or servlet are displayed in Chinese.
For example:
Submit the jsp code as follows:
<%@ page language="java" pageEncoding="utf-8"%><html><head><title>Enter form</title></head><body><form id="inputForm" name="inputForm" method="post" action="display.jsp"> Username: <input type="text" name="username"/><br/> Password: <input type="password" name="password"/><br/> <input type="submit" name="submit" value="submit"//</form></body></html>
The jsp code that receives parameters is as follows:
<% @ page language="java" pageEncoding="utf-8"%><html><head><tilte>Receive form</title></head><body> <% Insert request.setCharacterEncoding("utf-8"); %> Username: <%=request.getParameter("username")%><br/> Password: <%=request.getParameter("password")%><br/></body></html>Solution: Before receiving the post submitted parameters, use request.setCharacterEncoding("utf-8") to set the content format of the received parameters to utf-8 encoding. See the insert content in the receiving form. Of course, it is best to use Chinese filters for this kind of garbled problem.
The fourth case: URI method passes garbled parameters
Problem description: When passing parameters using a form, you can pass through post or get. The form is not applicable. You can also pass parameters using a link method. This method of passing parameters is essentially passing parameters as get. The parameters passed in this method may also appear garbled when they appear in Chinese.
like:
Copy the code as follows: <a href="/webproject/display.jsp?username=Zhang San&password=123">Show username and password</a>
Solution: The essence of the problem is that the default encoding method of the parameter content passed by get method is asked ISO8859-1, and using request.setCharacterEncoding("utf-8") cannot solve the problem. To solve this problem, modify the configuration file of the tomcat server. Modify line 43 of the conf/server.xml file in the tomcat directory:
Before modification:
<Connector port="8080" protocol="HTTP/1.1" maxThreads="150" connectionTimeout="200000" redirectPort="8443"/>
Modified content:
<Connector port="8080" protocol="HTTP/1.1" maxThreads="150" connectionTimeout="200000" redirectPort="8443" URIEncoding="utf-8"/>
This can solve this type of garbled problem.
The fifth situation:
Problem description: When using some class libraries or frameworks, in order to achieve internationalization of page content, the corresponding properties file needs to be written. The Chinese content in the properties file will also appear garbled when displayed.
Solution: This garbled problem can be solved by the native2ascii tool in jdk. Use the following command:
Copy the code as follows: native2ascii -encoding utf-8 display.properties display_zh_CN.properties
The reason for the garbled problem is that the java compiler can only handle character files encoded by Latin-1 or unicode.
The above is all about this article, I hope it will be helpful to everyone's learning.