Chinese characters are garbled during jsp to url

Source: Internet
Author: User

Chinese characters garbled during jsp tutorial to url
Encode in jsp before decoding in action

EncodeURI ("Chinese") encoding

URLDecoder. decode (request. getParameter ("parameter name"), "UTF-8"); complete decoding.

Example: url: "qtypenums. action? Typeid = <s: property value = 'typeid' escape = 'false'/> & qmobile = <s: property value = 'qmobile' escape = 'false'/> & qname = "+ encodeURI ('<s: property value = 'qname' escape = 'false'/> ') + "& qattr1 =" + encodeURI (' <s: property value = 'qattr1' escape = 'false'/> ') + "& qattr2 =" + encodeURI (' <s: property value = 'qattr2' escape = 'false'/> ') + "& qattr3 =" + encodeURI (' <s: property value = 'qattr3' escape = 'false'/> ')),


Decoding in action

String qname = this. getQname ();
String qattr1 = this. getQattr1 () = null? "": This. getQattr1 ();
String qattr2 = this. getQattr2 () = null? "": This. getQattr2 ();
String qattr3 = this. getQattr3 () = null? "": This. getQattr3 ();
String qname1 = "";
String qattr11 = "";
String qattr21 = "";
String qattr31 = "";
Try {
Qname1 = URLDecoder. decode (qname, "UTF-8 ");
Qattr11 = URLDecoder. decode (qattr1, "UTF-8 ");
Qattr21 = URLDecoder. decode (qattr2, "UTF-8 ");
Qattr31 = URLDecoder. decode (qattr3, "UTF-8 ");
} Catch (Exception w ){}

 

Use this method to resolve the URL characters you sent:

StringUrl is the garbled character set to be resolved!

     

New String (StringUrl. getBytes ("iso-8859-1"), ("gb2312 "));
You only need to convert the URL garbled characters to the Chinese character set and parse it with the "gb2312" Chinese character set!

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.
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 was introduced after webpage special effect 1.5, and escape was available in javascript1.0.

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.