Analysis of servlet Chinese garbled characters and Solutions

Source: Internet
Author: User
Document directory
  • Question 2
  • The problem code is as follows:
  • Supplement: Use the <meta> label to simulate the response header;
  • 1. POST request garbled
  •  
  • 2. garbled GET requests

 

I. Knowledge

 

1. GBK contains gb2312, that is, if gb2312 encoding is used, it can be decoded by GBK. Otherwise, it may not be true;

2. java. NiO. charset. charset. defaultcharset () obtains the default character encoding of the platform;

3. getbytes () is encoded using the default Character Set of the platform;

 

2. garbled Chinese Characters

 

When learning any technology, beginners often encounter Chinese garbled characters, such as MySQL, because it is not set during installation, and Chinese garbled characters are also encountered in servlet;

For example:

Outputstream out = response. getoutputstream ();

Out. Write (string );

Garbled characters may occur when outputting Chinese characters;

For example:

Protected void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {outputstream out = response. getoutputstream (); string data = "blog"; out. write (data. getbytes ("UTF-8 "));}

The output garbled problem is that the program uses UTF-8 encoding, And the browser uses gb2312 decoding, so there will be garbled;

 

Servlet garbled characters are divided into request garbled characters and response garbled characters;

 

 

3. Response Chinese garbled characters

 

An effective solution on the Internet is to add:

Response. setcharacterencoding ("UTF-8 ");

But I found another solution:

Respneuron. setheader ("Content-Type", "text/html; charset = UTF-8 ");

After filling in both sentences, I finally solved the problem;

In fact, we should think about the essence;

Question 1:

 

Here we will first explain the cause of the error, which is a flowchart showing garbled characters:

 

Response. setcontenttype ("text/html; charset = UTF-8 ");
The purpose is to control the browser behavior, that is, to control the browser with UTF-8 decoding;

Response. setcharacterencoding ("UTF-8 ");Is used for response. garbled error of the response stream output by getwriter (), if it is response. getoutputstream () does not require this solution, because it means to decode the data in the response object to the browser by decoding the UTF-8;

 

Solution flowchart:

 

 

Question 2 code:
Protected void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {printwriter out = response. getwriter (); string data = "blog"; out. println (data );}

Browser output :??

Cause: "Blog" is first encapsulated in the response object, because the text cannot be transmitted between IE and the Web server, and then encoded by the ISO-8859-1, but the ISO-8859-1 does not have the "blog" encoding, therefore, the output "?" Indicates no encoding;

 

Error Code flowchart:

 

 

The solution is: Response. setcharacterencoding ("gb2312"); set the code table used by Response

 

Solution flowchart:

 

 

Supplement: Use the <meta> label to simulate the response header;

<Meta http-equiv = "Content-Type" content = "text/html"/> is equivalent to response. setcontenttype ("text/html ");

4. Request garbled

 

Request requests are divided into post and GET requests. Different request methods have different solutions to solve Garbled text;

 

1. POST request garbled

 

Error cause:

 

Solution:

2. garbled GET requests

 

 

 

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.