URL parameter in GET request Chinese garbled problem

Source: Internet
Author: User

In the project will often encounter the messaging parameters, in the background to receive garbled problems. So in this case, how we should deal with the processing let us get to the background received parameters are not garbled is what we want to receive, here are some of my understanding and understanding.

A: Get request URL with Chinese parameters, there are three ways to deal with the prevention of Chinese garbled

1. If you are using Tomcat as a server, modify the Tomcat profile Conf/server.xml and add protocol= in <connector port= "8082" http/1.1 "uriencoding=" Utf-8 "Encoding Set

2, the front desk needs to encode the Chinese parameters, call the JS method encodeURI (URL), the URL encoding, and then request.

When the background is accepted, the string str = new String (Request.getparameter ("param") must be processed. GetBytes ("Iso8859-1"), "UTF-8");

Cause: When Tomcat does not set the encoding, the default is Iso8859-1, which is that Tomcat receives the get parameter by default in Iso8859-1 encoding. This is done by converting the parameter to a byte array with iso8859-1 encoding, and then converting the byte array to a string with UTF-8.

Also note that in the framework's use: request.setcharacterencoding (encoding), only valid for post requests. Also, Spring's characterencodingfilter only did the request (and response). setcharacterencoding (encoding); Therefore, the filter configuration of spring is not used for get parameter reception.

3, to solve the GET request, the background to accept the Chinese parameter garbled processing method (search function with parameters)

(1) Front desk access to data, in JS encoding processing

The encodeURI function uses the utf-8 to encode, but when the server decodes, the default is not to decode with uft-8, so it will appear garbled.

Two times encodeURI, the first time the code is the UTF-8 form of the URL, the second encoding is still the UTF-8 form of the URL, but the effect is equivalent to the first UTF-8 encoding (now all converted to ASCII characters), Once again, iso-8859-1 encoding, because the English characters say UTF-8 encoding and ISO-8859-1 encoding the same effect.

(2) Background decoding processing

When the parameters are received in the background, the first decoding via Request.getparameter () (possibly the character set of Gb2312,gbk,utf-8,iso-8859-1, which has no effect on the result) is obtained by the ASCII character. Then use UTF-8 for a second decoding, usually using the Java.net.URLDecoder ("", "UTF-8") method.

The process of encoding two times for two times is:

UTF-8 encoded->UTF-8 (iso-8859-1) encoding->iso-8859-1 decoding->utf-8 decoding, encoding and decoding process is symmetric, so there is no garbled.

Note:

1: This two times encodeURI method does not need to know the server decoding way, also can obtain the correct data.

2:get request is recommended as far as possible without Chinese parameters, if using the recommended use of two times encodeURI encoding

3. Reference URL encoding and two-time encodeuri:http://blog.sina.com.cn/s/blog_8af112fd0102vxb7.html

URL parameter in GET request Chinese garbled problem

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.