Transcoding and decoding when passing Chinese parameters in URLs

Source: Internet
Author: User

URL to pass the Chinese parameters when the several processing methods, summarized as follows:

1. Transcode The string: newstring ("xxxxx". GetBytes ("Iso-8859-1"), "Utf-8")

This transcoding method has great drawbacks because it uses the specified character set to encode this string into a byte sequence, stores the result in a new byte array, and then decodes the resulting byte array by using the specified character encoding to construct a new string string. This situation is likely to encounter the situation is that a Chinese character can not be completely decoded. In this way, the front can be displayed normally, but the last word may be garbled. So it is not recommended to use this method.

2. Transfer the code before the parameter, receive the parameters and then transcode back.

There are two types of this approach:

The first type:

Before the reference: Use Java.net.URLEncoder.encode ("xxxx", "Utf-8") to convert Chinese into 16 characters.

After receiving the parameters: Use Java.net.URLDncoder.decode ("xxxx", "Utf-8") to convert the 16 characters into Chinese.

It is important to note that when you use encode transcoding, special characters appear, and you need to replace the special characters with the corresponding 16 binary. Because special characters are passed as arguments in the URL path, they are garbled as well.

The second type:

Before reference: encodeURI ("xxxx").

After receiving the parameters: Use Java.net.URLDncoder.decode ("xxxx", "Utf-8") to convert the 16 characters into Chinese.

In this way, it is important to note that after using encodeURI transcoding, special characters will appear, when special characters are also transcoded, so the use of two times encodeURI, namely:

encodeURI (encodeURI ("xxxx")).

These two transcoding methods are very useful, so it is recommended that you use.

3. Modify the Tomcat configuration file:

In the Server.xml file in the Conf folder under the Tomcat installation directory, add the uriencoding= "UTF8" to the location where the access port is configured. <connector port= "8080" maxthreads= "All" minsparethreads= "maxsparethreads=", uriencoding= "GBK" >

JS to encode the text involves 3 functions: Escape,encodeuri,encodeuricomponent, the corresponding 3 decoding function: unescape,decodeuri,decodeuricomponent

1, passing parameters need to use encodeuricomponent, so that the combined URL will not be # and other special characters truncated.

For example: <script language= "JavaScript" >document.write (' <a href= ' http://passport.baidu.com/?logout&aid=7 & u= ' +encodeuricomponent ("http://cang.baidu.com/bruce42") + ' "> Exit </a& gt; '); </script>

2, the URL to jump when you can use the whole encodeURI

For example: Location.href=encodeuri ("http://cang.baidu.com/do/s?word= Baidu & Ct=21");

3, JS Use the data can use escape

[huoho.com Edit]

For example: Search the history record of the Tibetan.

4, Escape to the Unicode value other than 0-255 to encode the output%u**** format, in other cases escape,encodeuri,encodeuricomponent encoding results are the same.


The most used should be encodeuricomponent, which is to convert the Chinese, Korean and other special characters into the utf-8 format of the URL encoding, So if you need to use encodeURIComponent to pass parameters to the background, you need the background decoding to Utf-8 support (the encoding in form is the same as the current page encoding method)

Escape does not encode characters with 69: *,+,-,.,/,@,_,0-9,a-z,a-z

encodeURI does not encode 82 characters:!,#,$,&, ', (,), *,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,a-z

encodeURIComponent does not encode 71 characters:!, ', (,), *,-,.,_,~,0-9,a-z,a-z

URL encoding conversion in JavaScript, Escape () encodeURI () encodeuricomponent

When using a URL for parameter passing, some Chinese name arguments or URL addresses are often passed, and conversion errors occur during background processing. In some pass-through pages use GB2312, and the Receive page uses UTF8, so that the received parameters may be inconsistent with the original. URLs that are encoded using the server-side UrlEncode function are not the same as the URLs encoded with the encodeURI function of client JavaScript.

Coding methods in javascript:

Escape () Method:
Encodes the specified string using the ISO Latin character set. All space characters, punctuation marks, special characters, and other non-ASCII characters are converted to the character encoding in the%xx format (XX equals the encoded 16-digit number of the character in the character set table). For example, the encoding for a space character is%20. The Unescape method is the opposite. Characters that are not encoded by this method: @ */+



encodeURI () Method:

Converts the URI string into an escape format using the UTF-8 encoding format. Characters that will not be encoded by this method:! @ # $& * () =:/;? +


encodeURIComponent () Method:

Converts the URI string into an escape format using the UTF-8 encoding format. Compared to encodeURI (), this method encodes more characters, such as characters. So if the string contains several parts of the URI, it cannot be encoded in this way, otherwise the URL will display an error after the/character is encoded. Characters that will not be encoded by this method:! * ( )

Therefore, for the Chinese string, if you do not want to convert the string encoding format into UTF-8 format (such as the original page and the target page charset is consistent), only need to use escape. If your page is GB2312 or other encoding, and the page that accepts the parameter is UTF-8 encoded, it is necessary to use encodeURI or encodeuricomponent.


In addition, Encodeuri/encodeuricomponent was introduced after javascript1.5, and escape is available in the javascript1.0 version.

Transcoding and decoding when passing Chinese parameters in URLs

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.