Simple js instance processing url special symbols & amp; Processing

Source: Internet
Author: User

The parameter content in the url contains & yes, and I have two solutions.
One method is to use JS transcoding on the page. The example is as follows (frontend processing)
<A href = "#" onclick = "test ('$ {group. type}', '$ {group.cn}')" >$ {group.cn}) </a>
<Script language = "javascript">
Function test (a, B ){
Alert ("test ");
Location. href = 'groupdetailservlet? GroupTypeForDetail = '+ a +' & cn = '+ encodeURIComponent (B );
}
</Script>
The second method is: processing at the backend
1 >>>>>>>>>>>>>>>>>>>> jsp:
<A href = "groupsDetailServlet? Cn =$ {group. encodedCN} & groupTypeForDetail =$ {GroupType} "+ >$ {group.cn} </a>
2 >>>>>>>>>>>>>>>>>>>> java bean: group
String cn; // CN to be displayed
String encodedCN; // CN of the Parameter Data Transmission
Public Group (String cn) {this (); this.cn = cn; this. encodedCN = LdapUtil. encodeURLStr (cn );}
Public void setCn (String name) {this.cn = name; setEncodedCN (LdapUtil. encodeURLStr (cn ));}
Public String getCn () {return cn ;}
Public String getEncodedCN () {return encodedCN ;}
Public void setEncodedCN (String cn) {this. encodedCN = cn ;}

3 >>>>>>>>>>>>>>>>>>>>> call class to process decoding:
String cn = LdapUtil. decodeURLStr (encodedCN );
4 >>>>>>>>>>>>>>>>>>>> LdapUtil. java
Public static String encodeURLStr (String src)
{
Return src! = Null & src. trim (). length ()> 0? Src. replaceAll ("&","@"):"";
}
Public static String decodeURLStr (String src)
{
Return src! = Null & src. trim (). length ()> 0? Src. replaceAll ("@","&"):"";
}

-------------------------------------- The following is ----- network search information sharing ----------------------------------------------------------------------------
When url is used for parameter transfer, parameters or URL addresses of some Chinese names (or containing special characters) are often transmitted. Conversion errors may occur in background processing. In some transfer pages, GB2312 is used, and UTF8 is used on the receiving page. In this way, the received parameters may be different from the original ones. The URL encoded using the urlEncode function on the server is different from the URL encoded using the encodeURI function of the client javascript. Javascript code involves three functions: escape, encodeURI, and encodeURIComponent. The corresponding three decoding functions are: unescape, decodeURI, and decodeURIComponent.

The encoding method in javaScript:
Escape () method: uses the ISO Latin character set to encode the specified string. All space characters, punctuation marks, special characters, and other non-ASCII characters will be converted into character encoding in % xx format (xx equals to the hexadecimal encoding of this character in the character set table number ). For example, the space character is encoded as % 20. The unescape method is the opposite. Characters not encoded by this method: @ */+
EncodeURI () method: converts a URI string into an escape string in UTF-8 encoding format. Characters not encoded by this method :! @ # $ & * () = :/;? +'
EncodeURIComponent () method: convert a URI string to an escape string in UTF-8 encoding format. Compared with encodeURI (), this method will encode more characters, such. Therefore, if the string contains several parts of the URI, this method cannot be used for encoding. Otherwise, the URL will display an error after the/character is encoded. Characters not encoded by this method :! *()
Therefore, for a Chinese string, if you do not want to convert the string encoding format to the UTF-8 format (for example, when the charset of the original page and the target page is consistent), you only need to use escape. If your page is GB2312 or another code, and the page that accepts the parameter is UTF-8 code, use encodeURI or encodeURIComponent.
In addition, encodeURI/encodeURIComponent is introduced after javascript1.5, and escape is available in javascript1.0.
1. encodeURIComponent is required when passing parameters so that the combined url will not be truncated by special characters such. Example: <script language = "javascript"> document. write ('<a href = "http://passport.baidu.com /? Logout & aid = 7 & u = '+ encodeURIComponent ("http://cang.baidu.com/bruce42") +' "> exit </a> '); </script>
2. encodeURI can be used as a whole during url redirection. Example: Location. href = encodeURI ("http://cang.baidu.com/do/s? Word = Baidu & ct = 21 ");
3. You can use escape when js uses data. For example, search for the history record in the bucket.
4. When encoding unicode values other than 0-255, escape outputs % u *** format. In other cases, escape, encodeURI, and encodeURIComponent have the same encoding result.

The most commonly used format is encodeURIComponent, which converts special characters such as Chinese and Korean into UTF-8 url encoding, therefore, if you want to use encodeURIComponent to transmit parameters to the backend, you must use backend decoding to support UTF-8 (the encoding method in form is the same as that on the current page)
Escape unencoded characters are 69: *, +,-,.,/, @, _, 0-9, a-z, A-Z
EncodeURI is not encoded with 82 characters :!, #, $, &, ', (,), *, +,-,.,/,:,;, = ,? ,@,_,~ , 0-9, a-z, A-Z
EncodeURIComponent has 71 unencoded characters :!, ',(,),*,-,.,_,~ , 0-9, a-z, A-Z
The following are the special characters that may be used in the url and the encoded values in the url: (omitted)
The project found that encodeURI () encoding and conversion is directly performed on the parameter part of the url. When the backend servlet obtains the parameter through getParamater (), the correct value can be obtained directly without conversion. Note: The parameters are not in Chinese, and the framework uses the struts framework.

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.