- URL special characters need to be escaped
-
- 1. Replace spaces with plus signs (+)
- 2. Forward slash (/) separates directories and subdirectories
- 3. Question mark (?) separates URL and query
- 4. The percent sign (%) defines special characters
- 5. # Specify bookmarks
- 6, ampersand separated parameters
The reason for the escape character:
If your form is submitted using the get method, and there are special characters such as "&" in the submitted parameter, if it is not processed, the service side will treat the following & as another parameter. For example
, if the action of the form is list.jsf?act= Go &state=5
, the values of act and state can be obtained respectively through request.getParameter when submitting.
If your intention is the string act='go&state=5', then in order to get the exact value of act on the server, you must escape &
url escape character principle:
convert these special characters into ASCII codes , the format is: % plus the ASCII code of the character, that is, a percent sign %, followed by the ASCII (hexadecimal) code value of the corresponding character. For example the encoded value of space is "%20".
- URL special symbols and corresponding hexadecimal value encoding:
-
- 1. + The + sign in the URL represents a space %2B
- 2. Spaces Spaces in URLs can be encoded with a + sign or %20
- 3. / separates directories and subdirectories %2F
- 4. ? separates the actual URL from the parameter %3F
- 5. % specifies the special character %25
- 6. # means bookmark %23
- 7. & separator between parameters specified in URL %26
- 8. = The value of the specified parameter in the URL %3D
The solution is as follows (take the + sign as an example):
Method 1. Modify the client, and replace all the "+" in the parameters with "+" on the client with "2B%", so that the "+" can be obtained when the parameter is passed to the server.
Method 2: Modify the server side and replace the space with "+". This method is only applicable to the case where there is "+" in the parameter but no space.
example:
- String a = reuqest.getParameter("clientStr").replace(' ','+');
If the client is clientStr=test+OK, then the value of a is test+OK;
Method 3: Modify the server side, change the method for obtaining parameters from reuqest.getParameter to request.getQueryString().substring(0), and then parse the obtained string.
example:
- String a =request.getQueryString().substring(0);
If the client is clientStr=test+OK, then the value of a is clientStr=test+OK, which needs to be parsed again.
a=a.substring(10); The value of a is test+OK.
Attachment: A JS for escaping special characters in URLs.
- function URLencode(sStr)
- {
- return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
- }