Solution for ajax to submit to servelt to obtain garbled Parameters

Source: Internet
Author: User

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 ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.