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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 English version
Recommended: Win version, supports code prompts!
