Chinese code conversion in JAVA/JS

Source: Internet
Author: User
Tags decode all urlencode

One, Java, using UTF-8 to encode and use UTF-8 UrlEncode encoding results are different

UTF-8 "Junshan":& #x541B;& #x5C71;

Utf-8/urlencode "Junshan":%e5%90%9b%e5%b1%b1

String s = "Junshan";   = Urlencoder.encode (S, "UTF-8"); // %E5%90%9B%E5%B1%B1

Second, JS, using the function of the URL encoding

encodeURI ()/decodeURI (): UTF-8 encode/decode all except special characters in the URL.

var a = encodeURI ("Http://localhost:8080/abc/def/efg/Junshan &q=1;10+a ' b,cd#df");   var b = decodeURI (a);  

Third, JS, using the function of the URL encoding

encodeURIComponent ()/decodeURIComponent (): UTF-8 encode/decode all except special characters in the URL, more thorough characters than encodeURI.

var a = encodeURIComponent ("Http://localhost:8080/abc/def/efg/Junshan &q=1;10+a ' b,cd#df");   var

IV. encoding and decoding of URLs

Part of the URL:

http://localhost:8080/examples/servlets/servlet/Junshan? author= Junshan

PathInfo? QueryString

By default, browsers have different pathinfo and QueryString encodings (UTF-8 and GBK, respectively), which is not a small problem for server decoding.

Through the Tomcat source, learned that the URI portion of the URL is decoded by the character set is defined in Server.xml <connector uriencoding= "UTF-8" >, if not defined, the default is Iso-8859-1, So when there is Chinese in the URL (where Junshan is pathinfo), setting up Tomcat automatically decodes the URI in UTF-8 form, and the method in the controller can be located in Chinese requestmapping.

<port= "8080"  protocol= "http/1.1"  connectiontimeout = "20000" Redirectport = "8443" uriencoding = "UTF-8" />

The querystring of the get HTTP request and the form parameters to the post HTTP request are all saved as parameters. Both get the parameter values through Request.getparameter and decode them at the first call of the Request.getparameter method.

The querystring itself is passed through the HTTP header to the server , while the decoded character set is the charset defined in the header contenttype (the default ios-8859-1). To use the encoding defined in contenttype, you will <connector usebodyencodingforuri= "true"; Note: it does not decode the entire URI using bodyencoding. Instead of just using bodyencoding decoding for QueryString. In this case, you can pass the Chinese parameter.

<port= "8080"  protocol= "http/1.1"  connectiontimeout = "20000" Redirectport = "8443" uriencoding = "UTF-8" Usebodyencodingforuri = "true" /> <!--   -  

Finally, it can be seen that URL codec is more complex, difficult to control, the recommended URL is best not in Chinese.

Five, the post form of the codec

How to pass the parameters of the post form: The body of the HTTP

QueryString Parameter Passing Method: header of HTTP

Form submission with Contenttyep charset encoding, to the server is also used ContentType chartset decoding, so the general post form does not appear garbled.

Six, the body of the codec

String CE = request.getcharacterencoding (); Gets the server decoding method,

Request.getrequest.setCharacterEncoding ("UTF-8");//Set the server decoding mode

Chinese code conversion in JAVA/JS

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.