One, the occurrence of Chinese garbled in the case of the following three kinds:
1. Two types of submissions for form form are garbled in Chinese
①get Submit ②post Submission
2. Hyperlinks (essentially get commits) appear garbled in Chinese
3, Sendredirect (garbled)
Second, the cause of garbled characters:
Browser encoding Utf-8 sending a request to the server will result in inconsistent encoding
So in the sending request is the first definition of the code, it will be a good solution to the problem of coding
Three, to solve garbled several simple ways:
Post method submission: The solution is as follows: Request.setcharacterencoding ("Utf-8");
The Get method uses this method still does not solve the problem is still garbled, for Get submit the processing method used
Encapsulation becomes a tool class to handle:
Public class mytool{ publicstatic string getnewstring (String str) { string newstring =""; Try { newstringnew String (str.getbytes ("iso-8859-1"), "Utf-8"); } Catch (Exception e) { e.printstacktrace (); } return newstring;} }
Invoking the tool class:
String u = request.getparameter ("username");
mytool.getnewstring (U);
The way hyperlinks are submitted is the same as get commit processing
Four, browser-compatible version information issues, if IE6 or the following version, the second to third type will appear garbled
Hyperlink garbled and Sendredirect garbled
String Info=java.net.urlencoder encode ("hello. jpg", "utf-8");
<a href= "" + info> Test </a>
Response.sendredirect ("servlet address?") Username= "+ info);
Postscript:
On the server side is Chinese, in the response, but also to consider whether the browser is displayed correctly
Response.setcharacterencoding ("Utf-8");
Response.setcontenttype ("Text/html;charset=utf-8")
Java Web Chinese garbled processing note