Servlet Chinese garbled Problem and Solution Analysis, servlet Analysis
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:
[Java]View plaincopy
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:
[Java]View plaincopy
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