Solution for GET request garbled

Source: Internet
Author: User

The GET request is garbled and simulates a scenario that typically occurs.
Scenario One: Hyperlink <a href= "Url?name= Zhang San &age=18" >
Scene Two: Window.opon ("Url?name= Zhang San &age=18")

Solution One:
The JS code definition in the JSP:
UserName = encodeURI (UserName, "UTF-8");
defined in the Action class:
Get the key for the process definition
String userName = Elecuser.getusername ();
try {
userName = new String (username.getbytes ("iso-8859-1"), "UTF-8");
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
}

Solution Two:
Defined in the JSP:
UserName = encodeURI (UserName, "UTF-8");
UserName = encodeURI (UserName, "UTF-8");//convert Chinese into binary format
defined in the Action class:
Get the key for the process definition
String userName = Elecuser.getusername ();
try {
UserName = Urldecoder.decode (UserName, "UTF-8");
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
}

There are two kinds of characters appearing in the address bar, one is that the Chinese characters appear in the path part of the URL, one is the part that the Chinese characters appear in the URL, the second case must use the code to pass the parameter, and the way to accept the decoding method to complete the parameters. JS encoding has escape (), encodeURI (), encodeURIComponent () three commonly used methods, Escape () is often used in the submission of the page and processing the encoding format of the page is the same (for example, they are GB2312), The use of encodeURI () and encodeURIComponent () is basically the same, except that encodeURIComponent () is also "?". and other special characters to encode.
At the beginning of the Chinese parameters, using encodeURI () to pass the code, passed the past, found that the decoding problem, and thought might be the encoding method using the error, and then use the Escape () method, then found decoding thrown ishexdigit exception. With Baidu search Ishexdigit anomaly,
found that the escape () method caused the exception, and understand the browser delivery address of some principles, in the browser address bar, the browser thinks that% is an escape character, the browser will be between% and% of the encoding, two-bit two-bit out after decoding, and then passed to the processing page,
It is then decoded again by the processing page. From this I think of the use of the encodeURI method is correct, just need to use two times encodeURI method, such as encodeURI ("Chinese"), the first time is to encode Chinese into a%xy format,
The second time is to encode the% in the%xy, and the% code to%25. The whole process should be: The submission page uses encodeURI (encodeURI ("Chinese")) encoding, the final encoding results%25xy passed to the process of processing the page, the browser gets the URL address
(Note the Openmodeldialog method, the browser does not get the parameter encoding) after decoding into%xy, and then pass%XY to the processing page, processing the page using Urldecoder.decode ("parameter name"), " UTF-8 "); Complete decoding.
Summarize:
1, Chinese characters appear in the URL path part of the time do not need to encode and decode;
2, using encodeURI for 2 times encoding;
3, in the Openmodeldialog () Open mode form is not able to use Request.getparameter to get the parameters correctly;

The client and server can use filter filter to resolve character encoding problems when passing data, but filter can only resolve data submitted by post. For Get mode, you can use two times encodeURI (encodeURI ("Chinese")) and use Urldecoder.decode ("Chinese", "UTF-8") in the server;

Solution Three:

Modify the configuration file for server (tomcat) server.xml

However, in a distributed environment, this modification is not easy to expand.

Solution for GET request garbled

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.