Home>Article>Web Front-end> What to do if html post is garbled
html Solution to garbled post: First open the corresponding code file; then change the ISO encoded content passed by the post into UTF-8 format content.
The operating environment of this article: Windows 7 system, HTML5 version, DELL G3 computer
HTML uses the post method to submit Chinese content and the error solution is garbled
When I was doing an example today, I used the post method to submit the form. If there were Chinese characters, garbled characters would always appear when it was displayed on another page;
But the submission method will be When changed to get, this error will not occur.
See the picture and code below for detailed errors.
HTML code:
1 2 3 4 513 14测试Cookie的设置和获取 6 7 8
Servlet code, intercept a part:
1 response.setContentType("text/html;charset=utf-8"); 2 3 PrintWriter out=response.getWriter(); 4 5 String title="设置Cookie实例"; 6 String docType="\n"; 7 out.println(docType+ 8 "\n"+ 9 ""+title+" "+ 10 "\n"+ 11 ""+title+"
\n"+ 12 "
Error:
At the beginning I thought the code block was placed in the wrong position, so I put the above code in doPost and tried it, but this error still occurred.
So how to transmit Chinese using post method?
By searching for information,
submit by post
In this case, response.setCharacterEncoding has an impact. When response.setCharacterEncoding is not set, the value is null, and iso is used by default. -8859-1 to re-encode (decode).
The browser uses the encoding format of its own page as the starting encoding format, and encodes characters into bytes for transmission. When it comes to tomcat, tomcat does not interfere with the re-encoding (decoding) format. If response.getCharacterEncoding is null, then iso-8859-1 is used by default to re-encode (decode) characters. If set, characters are re-encoded (decoded) according to the set encoding format.
POST transmits single-byte data. Therefore, the data encoding transmitted by POST is ISO-8859-1 single-byte data. Therefore, English and numbers will not be garbled...Here In this case, the settings in the filter and server.xml are invalid. Of course, request.setCharacterEncoding() is also invalid because the principle of setCharacterEncoding is the same as that of the filter;
The correct way to deal with the above problem should be For:
String nameStr=new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
Change the ISO encoded content passed by post into UTF-8 format content, and then output it.
Recommended study: "HTML Video Tutorial"
The above is the detailed content of What to do if html post is garbled. For more information, please follow other related articles on the PHP Chinese website!