Chinese garbled characters of URL parameters in java, and Chinese garbled characters in javaurl

Source: Internet
Author: User

Chinese garbled characters of URL parameters in java, and Chinese garbled characters in javaurl

** Do students have various garbled characters when passing parameters through URLs? Passing parameters in a URL is a headache for programmers. Although it is possible to avoid using Chinese parameters, it is always unavoidable. My experience is summarized as follows :**

1. transcode the string: newString ("xxxxx". getBytes ("iso-8859-1"), "UTF-8 ")
This transcoding method has many drawbacks because it uses the specified character set to encode the String as a byte sequence and store the results in a new byte array, then, the byte array is decoded using the specified character encoding to construct a new String. In this case, it is possible that a single Chinese character cannot be completely decoded. In this way, the front can be displayed normally, but the last word may be garbled.
Therefore, this method is not recommended.

2. transcode before passing parameters. After receiving parameters, transcode them back.
There are two methods:
First:
Before passing parameters: Use java.net. URLEncoder. encode ("xxxx", "UTF-8") to convert Chinese to hexadecimal characters.
After receiving parameters: Use java.net. URLDncoder. decode ("xxxx", "UTF-8") to convert hexadecimal characters into Chinese characters.
In this way, special characters will appear after using encode transcoding. In this case, you need to replace the special characters with the corresponding hexadecimal notation. The special characters are garbled during parameter transfer in the url path.

Second:
Before passing the parameter: encodeURI ("xxxx ").
After receiving parameters: Use java.net. URLDncoder. decode ("xxxx", "UTF-8") to convert hexadecimal characters into Chinese characters.
Note that after using encodeURI for transcoding, special characters will appear. At this time, You Need To transcode the special characters, so use encodeURI twice, that is:
EncodeURI ("xxxx ")).

These two transcoding methods are very useful, so we recommend that you use them.

In the server. xml file in the conf folder under the Tomcat installation directory, add URIEncoding = "utf8" to the access port configuration.<Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" URIEncoding="GBK">

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.