There are many solutions for dealing with character encoding on the Internet. Here, I stand on the shoulders of my predecessors and make my own summary.
In my opinion, the solution to the garbled code problem is simply to set the encoding in three places:
1. Front-end pages such as HTML and JSP;
2. The request and response objects in the background servlet;
3. Server configuration file.
1. When encoding appears on front-end pages such as HTML and JSP, first check whether the Meta tag of a front-end page has the encoding set correctly. This Meta tag is also the header of the page
<span style="background-color: #ffff00; color: #000000"><%</span><span style="background-color: #f5f5f5; color: #000000">@ page language</span><span style="background-color: #f5f5f5; color: #000000">=</span><span style="background-color: #f5f5f5; color: #800000">"</span><span style="background-color: #f5f5f5; color: #800000">java</span><span style="background-color: #f5f5f5; color: #800000">"</span><span style="background-color: #f5f5f5; color: #000000"> import</span><span style="background-color: #f5f5f5; color: #000000">=</span><span style="background-color: #f5f5f5; color: #800000">"</span><span style="background-color: #f5f5f5; color: #800000">java.util.*</span><span style="background-color: #f5f5f5; color: #800000">"</span><span style="background-color: #f5f5f5; color: #000000"> pageEncoding</span><span style="background-color: #f5f5f5; color: #000000">=</span><span style="background-color: #f5f5f5; color: #800000">"</span><span style="background-color: #f5f5f5; color: #800000">utf-8</span><span style="background-color: #f5f5f5; color: #800000">"</span><span style="background-color: #ffff00; color: #000000">%><br></span>
2. Background request and response settings
2-1. When the background receives the URL request, if the request is not encoded, Then, the received request content will be garbled. At this time, two situations should be considered:
2-1-1. If it is a Get request, first obtain the parameters in the URL request, for example:
String method = request.getParameter("method");
Then call the String object to complete the conversion of parameter encoding,
String parseMethod = new String(method.getBytes("ISO-8859-1"),"UTF-8");
The String construction method here has two parameters: The form of the string is converted into a character array as the first parameter, and the second parameter is the encoding method of the converted string.
2-1-2. If it is a Post request, directly add a code to set the encoding before obtaining the request parameters, that is, call the setCharacterEncoding method of the request object to set the encoding:
request.setCharacterEncoding("UTF-8");
2.2. After the backend completes the business logic and persistence operations, it may be necessary to output the response stream data to the frontend. If the output content contains Chinese, You need to set the encoding of the response object. You can directly call the setContentType method of the response object:
response.setContentType("text/html;charset=utf-8");
3. Server configuration file settings
Current If the two-step setting still does not work, you should also find the server's configuration file server.xml in the conf folder in the server's installation directory, for example, mine is E:\tomcat7.0\conf, and set it. :
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
The above is the detailed content of How to solve the problem of garbled character encoding in JavaEE development. For more information, please follow other related articles on the PHP Chinese website!