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 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title>测试Cookie的设置和获取</title> 6 </head> 7 <body> 8 <form action="/TestTomcat/SetCookie" method="post"> 9 站点名:<input type="text" name="name"><br> 10 站点URL:<input type="text" name="url"><br> 11 <input type="submit" value="提交" /> 12 </form> 13 </body> 14 </html>
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="<! DOCTYPE html>\n"; 7 out.println(docType+ 8 "<html>\n"+ 9 "<head><title>"+title+"</title></head>"+ 10 "<body bgcolor=\"#f0f0f0\">\n"+ 11 "<h1 align=\"center\">"+title+"</h1>\n"+ 12 "<ul>\n" + 13 " <li><b>站点名:</b>" 14 + request.getParameter("name") + "\n</li>" + 15 " <li><b>站点 URL:</b>" 16 + request.getParameter("url") + "\n</li>" + 17 "</ul>\n" + 18 "</body></html>");
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!