Browser normal display
Response.setcontenttype ("text/html;charset= ' Utf-8 '")
Response.setcharacterencoding ("Utf-8");
Server-side Normal reception
There are three cases of garbled Chinese
1) Form submission (get mode, post mode): Input form entered as Chinese
2) hyperlink (get) Link? Parameter name = value (Chinese)
3) sendredict (get) link? parameter name = value (Chinese)
The root cause of garbled production
Request send: The browser has the default encoding, after the above settings, is utf-8.
When the Web server receives, there is also an encoding, because the server is generally developed abroad, so the default encoding is generally iso-8859-1
Browser and server encoding method is inconsistent, resulting in garbled
Problem Solving Methods
1, if the form is submitted, the method is post (form submission should try to select Post)
The parameters are passed through the HTTP request body, and the Request.setcharacterencoding method is used to specify how the Web server accepts the encoding, such as request.setcharacterencoding ("Utf-8")
2, if it is a form submission, the method is get
At this point pass the parameter through the link, for garbled case, can use
String Value=new string (request.getparameter ("parameter name"). GetBytes ("Iso-8859-1"), "Utf-8") to resolve
This scenario can be encapsulated as a tool class
Package Com.bobo.utils;import java.io.UnsupportedEncodingException; Public classMyTools { Public Staticstring getnewstring (String str) {Try { return NewString (Str.getbytes ("iso-8859-1"),"Utf-8"); } Catch(unsupportedencodingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); return NULL; } }}
garbled Tool class
3, Hyperlink garbled, sendredict garbled is essentially a GET request, the solution with 2
4, if it is IE6 and the following version, the above method is related to the Get method is still possible garbled, this time the solution is:
Encode the Chinese that you want to send
String info=java.net.urlencoder.encode ("Hello", "utf-8");
Response.sendredict ("URL path? parameter name =" +info);
A third file is received
String Info=new string (request.getparameter ("parameter name"). GetBytes ("Iso-8859-1"), "Utf-8");
Add a point of knowledge:
When we download the file, the warning box may be garbled in Chinese, at this time the solution is:
String temp=java.net.urlencoding.encode ("Chinese file name", "Utf-8")
Response.setheader ("Content-disposition", "attachment;filename=" +temp);
The Chinese garbled processing in Java EE