search
HomeJavaJavaBaseJava submission garbled code solution

Java submission garbled code solution

Dec 17, 2019 pm 02:54 PM
javaGarbled characters

Java submission garbled code solution

Solution to java submission of garbled characters:

1. If the submission method is post, and you want to avoid garbled characters, you only need to set the encoding of the request object on the server side. The client Which encoding is submitted by the client, the server-side request object is received in the corresponding encoding. For example, if the client is submitted in UTF-8 encoding, then the server-side request object is received in UTF-8 encoding (request.setCharacterEncoding("UTF -8"))

2. If the submission method is get, setting the encoding of the request object is invalid. The request object still receives data in the default ISO8859-1 encoding. Therefore, if you want to avoid garbled characters, you can only After receiving the data, manually convert it. The steps are as follows:

1). Obtain the data submitted by the client, and what you get is a garbled string, data="???è?????"

String data = request.getParameter("paramName");

2). Search the ISO8859-1 code table and get the byte array of the original data submitted by the client

byte[] source = data.getBytes("ISO8859-1");

3). Construct characters with the specified encoding through the byte array String, solve garbled characters

data = new String(source, "UTF-8");

Construct a string with the specified encoding through a byte array. The encoding specified here is based on the character encoding used by the client when submitting data. If it is GB2312 , then set it to data = new String(source, "GB2312"), if it is UTF-8, then set it to data = new String(source, "UTF-8")

3. Client If you want to transmit data to the server, you can submit it through a form, or you can add parameters after the hyperlink. For example:

<a href="${pageContext.request.contextPath}/servlet/RequestDemo05?userName=gacl&name=房明">点击</a>

Click the hyperlink, and the data is transmitted to the server in the form of get, so receive The problem of Chinese garbled characters will also occur when using Chinese data. The method to solve the problem of Chinese garbled characters is the same as the above-mentioned method of handling the problem of garbled Chinese data when submitting forms in the get method, as shown below:

String name = request.getParameter("name");
name =new String(name.getBytes("ISO8859-1"), "UTF-8");

In addition, there are other things that need to be mentioned One point is that if the URL address is followed by Chinese data, then the Chinese parameters are best processed using URL encoding, as shown below:

<a href="${pageContext.request.contextPath}/servlet/RequestDemo05?userName=gacl&name=<%=URLEncoder.encode("房明", "UTF-8")%>">点击</a>

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

The above is the detailed content of Java submission garbled code solution. 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

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.