Home > Java > javaTutorial > body text

How to solve the problem of garbled character encoding in JavaEE development

巴扎黑
Release: 2017-07-21 17:03:20
Original
1669 people have browsed it

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>
Copy after login

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");
Copy after login

  Then call the String object to complete the conversion of parameter encoding,

String parseMethod = new String(method.getBytes("ISO-8859-1"),"UTF-8");
Copy after login

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");
Copy after login

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");
Copy after login

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"/>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!