On solving the problem of garbled characters and the methods in HttpServletResponse

Source: Internet
Author: User
Tags html code example html form

Statement: The material used in this column is written by the VIP students of the Kay Academy, who has the right to be anonymous and has the final right to interpret the article; The Edith Academy is designed to promote students to learn from each other on the basis of public notes.

On solving the problem of garbled characters

There will be garbled phenomenon, in fact, because the character set encoding inconsistency, as if the Chinese people talk with foreigners, do not understand each other what is said. Character set encoding is also the case, it is a piece of GBK encoded text, but to use UTF-8 encoding format to decode, of course, is're same page will appear garbled, this time to use GBK encoded format to decode the problem. If each other is the use of GBK encoding, it is like Chinese and Chinese people speak Mandarin, can understand the other side of what is said, so that there is no garbled.
In web development, the request or response data garbled, often is the client and the service side of the problem of coding inconsistencies.
But before we introduce how to solve the problem of garbled, let's take a look at some of the ways to get form data in HttpServletRequest, although in the previous article also introduced the use, but about garbled and get the specific value of this aspect does not involve:

Gets and sets the form data method (the data in the file cannot be obtained if the file is uploaded):

Since it's about the form, you'll have to write a simple HTML form code, and we can create an HTML file in eclipse:

It may be inconvenient to write HTML code using Eclipse, and we can use a tool that specifically writes HTML code to write HTML files that have already been created in eclipse, and I use Hbuilder as an example:
1. Copy the path to the directory where the HTML files in Eclipse are located:

2. Click on the file in the Hbuilder and select Open Directory to paste the copied file path and a new name for the project:

The project catalog is as follows:

, you can see that index.html is already under this project, we can edit this HTML file in Hbuilder, the edited content will be synced to eclipse, because both of them are accessing the same directory as the same HTML file.

3. The code I edited in Hbuilder is as follows:

4. Take a look at what happened to eclipse:

You can see that the code is synchronized.

Browser Run Results:

Here are some common ways to get form data using the actual code, code examples:

To execute an HTML file in Eclipse, Eclipse has a built-in browser:

If you want to use this URL address in another browser:

Http://localhost:8080/TestResponse/index.html

Do not run this HTML file directly in Hbuilder because its URL refers to the Hbuilder's engineering path.

Console Print Results:

, you can see that we've got all the values.

There is a problem when getting form data: When you need to get the value of a property, if you get a null result, it is because there is no such attribute in the form data. For example, I get a property that doesn't exist:

Console Print Results:

You can see that the result is null, so when you get the form data for some action, there is a null pointer exception, most likely because the code is wrong to get a non-existent property.

If a property value in the form data is not written, then an empty string, not NULL, is obtained, for example:

Console Print Results:

, and does not print null, but print blank, this blank is an empty string: '

garbled situations, and workarounds:

Now let's revise the code to change the way the form is submitted to post, run it again, and look at the print results of the console, HTML code example:

Java code example:

Submitted form:

Print results for the console:

You can see that the printed results in the console appear unrecognized characters, the workaround is simple, using the setcharacterencoding (String) method, set the encoding format of the data submitted by the form:

Operation Result:

Note: In addition to the need to set the encoding format in the Java code, in the HTML file also to set the encoding format, if the HTML does not set the encoding format, even in the Java code using the Setcharacterencoding (String) method set is not used, So this is two-way, for example I delete the HTML file in the encoding format of the label:

You can see that the display on the Web page is garbled (this is because of the browser reason that is built into eclipse, generally the browser preset the character encoding in advance, so this does not happen)

Console Print Results:

As expected, there are unrecognized characters, so the HTML file also needs to set the encoding, otherwise it will appear garbled.

Here's how you can display Chinese in the address bar of the browser:

This is actually because the browser transcoding, you can copy this URL into Notepad:

Can see is a bunch of code, and does not display Chinese, so actually the browser is to convert this code to Chinese only.
As long as the characters that are not part of the 128-character characters are converted to this format in the address bar, these are encoded in the 16-encoded format, which is encoded in the following text sample:

, each of the 16 binary encodings is preceded by a%, which is utf-8 encoded in Chinese, so a medium text corresponds to 3 16 binary encodings.
If the GBK encoding format in Chinese is a medium text corresponding to 2 16 encoding, but the GBK encoding format converted to 16 encoding can not be converted by the browser, will still show the 16 binary encoding:

16 Encoding for Chinese characters:

, the GBK encoding format of the Chinese characters and utf-8 encoded in the text is not the same, is 2 16 encoding corresponding to a Chinese text.

Regarding the client requests the data aspect the garbled situation to introduce so many, in addition the response data garbled situation and the solution method in the Introduction HttpServletResponse method part to explain.

Mind Mapping:

Methods in the HttpServletResponse

The object of the HttpServletResponse interface type is to encapsulate the service-side response data, so the methods in this object are related to the response data. Here are some common ways to do this:

Here's how to use the above method using the actual example:

To edit a response header type of method:

code example:

It is necessary to set the encoding format of the response data on the server side, and the same is to avoid garbled problems. For example, I don't set the encoding format of the response data and output a Chinese text to see what happens, code examples:

Operation Result:

, you can see that the output Chinese will not be recognized without setting the encoding format of the response data.
This problem set the response data encoding format is good, but the server-side settings encoding format, to the browser-side encoding format corresponds to, if the wrong should still be garbled, code example:

Operation Result:

To add a new response header data:

code example:

Opening the TCP/IP monitor window, you can see the above code added to the response header data:

Get the response header information for the setting:

code example:

Console Print Results:

To modify the response header information:

code example:

TCP/IP Monitor window:

Summarize:
To solve the problem of garbled client form submission data, it is necessary to use the setcharacterencoding (String) method to set the encoding format corresponding to the client.
To solve the problem that the service side response data is garbled, use the setcharacterencoding (String) method to set the corresponding encoding format.
HttpServletRequest is the object that encapsulates the request data, so its methods are related to requesting information from the client.
HttpServletResponse is an object that encapsulates the response data, so its methods are related to the service-side response information.

On solving the problem of garbled characters and the methods in HttpServletResponse

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.