Urlencoder.encode, Urldecoder.decode, Escape, encodeURI, encodeURIComponent

Source: Internet
Author: User
Tags alphanumeric characters

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, and if it is Chinese, the Unicode encoding format, such as%uxxxx, is used. The Unescape method is the opposite. There are 69 characters that will not be encoded by this method: @*_+-./, 0-9,a-z,a-z

encodeURI () method

Converts the URI string into an escape format using the UTF-8 encoding format. encodeURI does not encode characters of 82: [email protected]#$&* () _+:?-=; ',./,0-9,a-z,a-z

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 71: ~!* () _-'., 0-9,a-z,a-z

Special character conversion in post mode

Just tell me. When a form is submitted as a post, some characters in the value of the form element are also converted, but the following do not convert: [email protected]*_-., A-Z-A-0-9, the characters to be converted are encoded first by the browser encoding, The code is then converted into a%xx (xx two-bit hexadecimal) Form parameter string and sent to the server.

Urlencoder.encode (string s, String enc)/urldecoder.decode (string s, String enc) encoding in Java

Urlencoder.encode (string s, String enc)/urldecoder in Java. Decode (string s, String enc) mechanism: they are not encoded in the same way as the encodeURI and encodeURIComponent functions in JavaScript. encodeURI, encodeuricomponent use is UTF-8, is cannot change. But the codec function in Java is not necessarily UTF-8, where the encoding and running environment is related, by default, under the Tomcat5.5 is iso8859-1 to decode the URL passed in the parameters, but you can also set the URL encoding method, such as It is possible to set the Uriencoding and Usebodyencodingforuri properties in Tomcat, for example, see the Urlencoder.encode () section in the analysis of HTTP bitstream and JSP garbled (i).

When Urlencoder.encode (string s, String enc) encodes a string, the following rules are used:

    • The alphanumeric characters " a to" z , "to" A and "to" Z remain unchanged 0 9 .
    • Special characters "," "," " . - and" * _ "remain unchanged.
    • The space character "   " is converted to a plus sign " + ".
    • All other characters are unsafe, so you first use some encoding mechanisms to convert them to one or more bytes. Each byte is then represented by a 3-character string " %xy  ", where xy is the two-bit hexadecimal representation of the byte. The recommended encoding mechanism is UTF-8. However, for compatibility reasons, if an encoding is not specified, the default encoding for the corresponding platform is used.

For example, using the UTF-8 encoding mechanism, the string "The Stringü@foo-bar" will be converted to "The+string+%c3%bc%40foo-bar" because in UTF-8, the character U is encoded as two bytes, C3 (hex) and BC (16 in Character @ is encoded as a byte 40 (hexadecimal).

The Urldecoder.decode (string s, String enc) conversion process is exactly the opposite of the process used by the Urlencoder class. Assume that all characters in the encoded string are one of the following: "To", "to", "to" and ",", "", and " a z A Z 0 9 - _ . * . The " % " character is allowed, but it is interpreted as the start of a special escape sequence.

The following rules are used in the conversion:

    • The alphanumeric characters " a to" z , "to" A and "to" Z remain unchanged 0 9 .
    • Special characters "," "," " . - and" * _ "remain unchanged.
    • The plus sign " + " is converted to the space character "   ".
    • The " %xy  " format sequence is treated as a byte, where xy is a two-bit hexadecimal representation of 8 bits. Then, all substrings that contain one or more of these byte sequences consecutively will be replaced by characters whose encoding can generate these contiguous bytes. You can specify the encoding mechanism for decoding these characters, or, if not specified, the default encoding mechanism of the platform.

Note: The following code in the cookie written in Chinese must first Urlencoder.encode encoding, when reading a cookie with Urldecoder.decode encoded after conversion to Chinese

<%
String name = "CSS Yellow";
Cookie CK = new Cookie (Urlencoder.encode ("username"), Urlencoder.encode (name));
Ck.setmaxage (24*1000);
Response.addcookie (CK);
Out.println ("Write Cookie Complete");


cookie[] cookies = request.getcookies ();
for (Cookie c:cookies) {
if (C.getname (). Equals ("username"))
{
Out.println (Urldecoder.decode (C.getvalue ()));
}
}
%>

Original: Http://hi.baidu.com/suofang/item/40135d26e992820876272cea

Urlencoder.encode, Urldecoder.decode, Escape, encodeURI, encodeURIComponent

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.