Home > Java > JavaBase > Introduction to several garbled code processing methods in Java

Introduction to several garbled code processing methods in Java

Release: 2019-12-10 15:34:16
Original
4066 people have browsed it

Introduction to several garbled code processing methods in Java

java garbled solution:

Solution 1 (if there are many garbled parameters, it will be less efficient)

The default encoding of the browser is ISO-8859-1. The back-end Servlet receives the request parameters, decodes them according to ISO-8859-1, obtains a binary stream, and then encodes it with UTF-8.

Sample code:

// 获得请求参数
String queryString = request.getQueryString();
// 按照ISO-8859-1方式解码
byte[] bytes = queryString.getBytes("ISO-8859-1");
// 按照UTF-8编码
String param = new String(bytes,"UTF-8");
Copy after login

Solution 2 (This method only supports get request)

Modify the server ( tomcat) configuration: In server.xml, the default is ISO-8859-1 (not shown), we can add UTF-8

Introduction to several garbled code processing methods in Java

solution 3 (This method only supports post request)

Set the request parameter encoding directly in the servlet--recommended

//设置请求参数编码 
request.setCharacterEncoding("UTF-8");
Copy after login

If post and get appear garbled at the same time, combine method 2 and method 3. Use

to respond with garbled characters - to respond to

 // 设置MIME类型
response.setContentType("text/html");
// 设置编码
response.setCharacterEncoding("UTF-8");
//设置编码(简写)
response.setContentType("text/html;charset=utf-8");
Copy after login

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of Introduction to several garbled code processing methods in Java. 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