URL special character escaping and solving method

Source: Internet
Author: User

URL special characters to be escaped

    1. 1. Replace the space with a plus sign (+)
    2. 2. Forward slash (/) separates directories and subdirectories
    3. 3. Question mark (?) Separating URLs and queries
    4. 4, percent semicolon (%) make special characters
    5. 5. #号指定书签
    6. 6, & Separation parameters

The reason for the


Escape character is:  

If your form is submitted with a Get method and a special character such as "&" is present in the argument, the service side will treat the & as a different parameter if you do not do the processing. For example  
The action of the form is list.jsf?act=go&state=5 
the value of Act and state can be obtained separately by Request.getparameter when committing.  
If your intention is act= ' go&state=5 ' This string, then in order to get the exact value of Act on the server, you must escape the &  

URL escape character principle:  
The
converts these special characters into ASCII code in the form of the ASCII code of the% plus character, which is a percent percent, followed by the ASCII (16 binary) code value of the corresponding character. For example, the encoded value of a space is "%20".  
 

    1. URL special symbol and corresponding hexadecimal value encoding:
    2. 1. + URL + sign for space%2b
    3. 2. Spaces in the space URL can be used with the + number or code%20
    4. 3./separate directories and subdirectories%2f
    5. 4.? Separating the actual URLs and parameters%3f
    6. 5.% Specify special characters%25
    7. 6. # means bookmark%23
    8. 7. The delimiter between the parameters specified in the & URL%26
    9. 8. = value of the specified parameter in the URL%3d

The workaround is as follows (with the + number as an example):

Method One, modify the client, the client with "+" in the parameters of "+" all replace with? " 2b% ", so that when the parameter is passed to the server, it can get" + ".

Method Two, modify the server side, the space is replaced with "+", this method only applies to the parameters have? " + "There are no spaces in the case.

Example:

    1. String a = reuqest.? GetParameter ("Clientstr")? Replace (', ' + ');

If the client is Clientstr=test+ok, then the value of a is test+ok;

Method Three, modify the server side, the method to get the parameters from the reuqest. GetParameter instead of request.getquerystring (). substring (0), and parse the resulting string.

Example:

    1. ?? String a =request.getquerystring (). substring (0);

? If the client is Clientstr=test+ok, then the value of a is? Clientstr=test+ok, you need to parse it again,

A=a.? SUBSTRING (10); Gets the value of a to Test+ok.

Attached: A JS that is used to escape special characters in the URL.

    1. ? function UrlEncode (SSTR)
    2. {
    3. Return Escape (SSTR). Replace (/\+/g, '%2b '). Replace (/\ "/g, '%22 '). Replace (/\ '/g, '%27 '). Replace (/\//g, '%2f ');
    4. }

URL special character escaping and solving method

Related Article

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.