HTML uses the post method to submit Chinese content with garbled characters,
In today's example, the form is submitted using the post method. If there is a Chinese character, garbled characters are always displayed on the other page;
However, this error will not occur when you change the submission method to get.
For detailed errors, see the image and code below.
HTML code:
1 <! DOCTYPE html> 2
Servlet code, part of the screenshot:
1 response. setContentType ("text/html; charset = UTF-8"); 2 3 PrintWriter out = response. getWriter (); 4 5 String title = "set Cookie instance"; 6 String docType = "<! DOCTYPE html> \ n "; 7 out. println (docType + 8 "
Error:
At first, I thought that the code block was not placed in the correct position. I put the above Code in the doPost for an interview. This error still occurred.
So how to use post to pass Chinese characters?
Search for information,
Post submission
In this case, response. setCharacterEncoding has an effect. If the value is null when response. setCharacterEncoding is not set, the iso-8859-1 is used by default for re-encoding (Decoding ).
The browser uses the encoding format of its own page as the starting encoding format to encode characters into bytes for transmission. tomcat will not interfere with the re-encoding (Decoding) format. If response. getCharacterEncoding is null, The iso-8859-1 is used by default for re-encoding (Decoding) into characters. If it is set, reencoding (Decoding) characters according to the set encoding format.
In the past, POST was used to transmit single-byte data. so the data encoding sent from POST is the single-byte data of the ISO-8859-1. therefore, there will be no garbled characters in English and numbers... in this case. filter and server. the settings in xml are invalid. of course, request. setCharacterEncoding () is also invalid because the setCharacterEncoding principle is the same as the filter;
The correct solution to the above problem should be:
1 String nameStr=new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
Change the ISO encoded content passed by post to the content in UTF-8 format, and then output.
See http://blog.csdn.net/sxyandapp/article/details/44623039.