The encodeURI of the solution of the URL to the Chinese parameter leading to garbled

Source: Internet
Author: User

when the Chinese parameter is passed through the URL, the value obtained in the backend of the server is often garbled , and there are many solutions, this article mainly introduces how to solve the problem of Chinese garbled by encodeURI:

First: The parameters of the front-end delivery of the Chinese parameters need to do two encodeURI processing:

? var Requesturl = ' url?rolename= ' +encodeuri (encodeURI ("Rowobj.appname"));

Note : Rowobj.appname? A Chinese-language string that will be uploaded to the background

second: In the server-side daemon code to be decoded with Java.net.Decode, to get the Chinese:

String appname= Java.net.URLDecoder.decode (Request.getparameter ("RoleName"), "UTF-8");

Two-step fix!

However, why the front-end to do two times encodeURI??? The reasons are as follows:

1, encodeURI function is mainly to the URI to do transcoding, it is the default to use the Utf-8 encoding;

2, the general view, Chinese characters in the Utf-8 is generally 3 byte composition, each byte will be converted to 16 encoding, plus the% number;

Assuming that the page needs to be uploaded to the background in Chinese is a "medium" word, follow the following procedure to go through:

1) for the first time, encodeURI, in accordance with the utf-8 way to get the number of bytes into [ -28,-72,-83], the byte-code array to traverse, each byte into a corresponding 16 binary number, so it becomes [E4,b8,ad], and eventually become [% e4,% b8,% AD] ( Note: Please remove the% and the middle of the encoding space, I write here you see is garbled, the following is the same), at this time there is no multibyte character, all are single-byte characters.

2) The second encodeURI encoding, will be considered as an escape character, and do not encode the character after the%, will be changed to%?25, so just the array becomes the [% e4,% b8,% ad], and then the processed [% e4,% b8,% AD] sent to the server, when the Application server tuning When using the GetParameter method, the GetParameter method requests parameters from the application server, but the application server initially receives the [% e4,% b8,% AD] from the front end, and the Application Server container defaults to the code once. The default encoding used by the container is the default encoding of the container, either UTF-8,GBK, or ISO-8859, can get [% e4,% b8,% AD], because the% will be parsed, and return this value to the GetParameter method;

3) The final use of the Java.net.Decode using UTF-8 encoding decoding, you can get "medium" word;

So, if it was just coded once, when it is easy to automatically decode (the default solution once the code), if it is according to ISO-8859 to decode UTF-8 encoded things and then return to the GetParameter method is garbled.

Core Code :

Js:? encodeURI (encodeURI ("Rowobj.appname"))

Java:java.net.URLDecoder.decode (Request.getparameter ("RoleName"), "UTF-8"); ?

?

Personal summary, only for reference, if there are questions, welcome to the Great God advice, in addition, thanks to two references, address:

? http://blog.csdn.net/howlaa/article/details/12834595

Http://www.tuicool.com/articles/fuqIBju

The encodeURI of the solution of the URL to the Chinese parameter leading to 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.