search
HomeJavaJavaBaseIntroduction to several garbled code processing methods in Java

Introduction to several garbled code processing methods in Java

Dec 10, 2019 pm 03:34 PM
javaGarbled characters

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");

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");

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");

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!