Here, we assume that the reader understands the basic
Ajax
Technology or more. I will only elaborate on one phenomenon, one solution; reason,
The principle is also unclear.
......
Let's start with a simple process,
Upload servlet values to JSP page js
In the script, the related values are displayed on the page. It is easy to solve Chinese garbled characters in this direction (servlet-javascript. You only need
Add
Encoding settings:
Copy codeThe Code is as follows:
Response. setContentType ("text/html; charset = UTF-8 ");
And JSP page encoding mode can be set freely, GBK, GB2312, UTF-8, of course
ISO-8859-1 is not good, at least you need to display Chinese on the page.
From the js script to the servlet, the problem is that the value transfer method (GET and POST) is different in the open () method of the xmlHttp object, which is divided into two situations:
Before introducing this question, let's take a look at the differences between the two methods:
Assume that the value "Software Engineering" is passed, and the variable name is "selcol ".
1. GET method:
Copy codeThe Code is as follows:
Function selectCol (){
CreateXMLHttpRequest ();
Var selcol ="
Software Engineering
Copy codeThe Code is as follows:
";
Var url = "/emp/FindSpecial? Selcol = "+ selcol;
XmlHttp. onreadystatechange = handleStateChange;
XmlHttp. open ("GET", url, true );
XmlHttp. send (null );
}
2. POST method:
Copy codeThe Code is as follows:
Function selectCol (){
CreateXMLHttpRequest ();
Var selcol = "selcol = Software Engineering ";
Var url = "/emp/FindSpecial ";
XmlHttp. onreadystatechange = handleStateChange;
XmlHttp. open ("POST", url, true );
XmlHttpxmlHttp. setRequestHeader ("Content-type", "application/x-www-form-urlencoded ");
XmlHttp. send (selcol );
}
If the GET method is used for transfer, the servlet will convert the Code as follows when getting the value:
Copy codeThe Code is as follows:
String selcol = new String (request. getParameter ("selcol"). getBytes ("ISO-8859-1"), "GBK ");
Turn GBK, GB2312 OK, turn UTF-8 can not!
At this time, you may find that these two statements appear at the same time:
Copy codeThe Code is as follows:
Response. setContentType ("text/html; charset = UTF-8 ");
String selcol = new String (request. getParameter ("selcol"). getBytes ("ISO-8859-1"), "GBK ");
I personally understand this: the first sentence is only guaranteed, from the servlet passed out of the data encoding method for UTF-8;
The latter converts the transmitted data into GBK-encoded data. Easy to identify and process.
If the POST method is used for transfer, then when we get the servlet value, the same as above, just convert the encoding to the UTF-8,
At this time, GBK and GB2312 won't work!
Copy codeThe Code is as follows:
String selcol = new String (request. getParameter ("selcol"). getBytes ("ISO-8859-1"), "UTF-8 ");