Decoding and encoding of http sent data

Source: Internet
Author: User

 

1. In general, the Chinese characters in the directory in the url are encoded according to UTF-8, And the UTF-8 encoding in "medium" is "% E4 % B8 % AD ".

2. the get method parameters are encoded according to the computer's system language. The Chinese language is GBK, And the GBK of "medium" is encoded as "% D6 % D0"; the Japanese environment is encoded as "shift-jis ", "medium" is encoded as "% 92% 86"

3. Parameters in the post mode will be placed in the http body and encoded according to the content-type in the jsp page.

For a backend decoding:

1. If the matching path in the background contains Chinese characters, decode the Chinese character "medium" in the path in the correct format. The solution can also be:

Add URIEncoding = "UTF-8" to the Connector node of tomcat server. xml to match the Chinese path.

Generally, the path does not contain Chinese characters. In addition, adding URIEncoding = "UTF-8" to tomcat will affect all projects. Therefore, this method is generally not used (I checked it based on my interest ).

2. decode the get parameter correctly.

In tomcat does not add URIEncoding = "UTF-8", tomcat by default according to the iso-8859-1 decoding, so to get the correct Chinese parameters, according to the following method, first decoding and then according to the correct encoding:

System. out. println (new String (request. getParameter ("username"). getBytes ("iso-8859-1"), "parameter encoding "));

Parameter encoding is the encoding of your computer system environment.

If URIEncoding = "UTF-8" is added, theoretically the parameters are decoded according to UTF-8. the GBK-encoded D6D0 does not match the "medium" character in UTF-8, and other characters may appear, therefore, the binary value corresponding to the parameter is changed. Therefore, no matter how the encoding and decoding are performed, the following error occurs:

System. out. println (new String (request. getParameter ("username"). getBytes ("UTF-8"), "parameter encoding "));

System. out. println (new String (request. getParameter ("username"). getBytes ("iso-8859-1"), "parameter encoding "));

It hasn't been solved yet. Please help me.

During the experiment, the Chinese characters in the path and the Chinese characters in the parameters adopt different encodings. We can use javascript to encode the parameters, because encodeURI () the method will encode the parameters according to UTF-8, so ajax can be used for implementation (this is only a solution. If you are interested, try it ).

[Javascript] view plaincopy
<Script language = "javascript" type = "text/javascript">
Var request = false;
Try {
Request = new XMLHttpRequest ();
} Catch (trymicrosoft ){
Try {
Request = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (othermicrosoft ){
Try {
Request = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (failed ){
Request = false;
}
}
}

If (! Request)
Alert ("Error initializing XMLHttpRequest! ");

Function getCustomerInfo (){
Var username = document. getElementById ("username"). value;
Alert (username );
Var url = encodeURI ("../students/1? Username = "+ username );
Request. open ("GET", url, true );
Request. send (null );
}
Alert ("sdfsdf ");
</Script>

In this case, "medium" in username = will be encoded in UTF-8, thus avoiding the system encoding used for direct form submission. The Chinese system encoding is gbk, and the Japanese is shift-jis.

 

3. The post decoding URIEncoding = "UTF-8" setting has no effect on post, so the post parameter is decoded in the background according to the iso-8859-1:

String s = new String (t. getParameter ("username"). getBytes ("ISO-8859-1"), "UTF-8 ");

In this way, Chinese characters can be output.

If you do not want to perform this conversion every time, you can use filter in spring. Filter takes effect on post and does not apply to get.

<Filter>

<Filter-name> encodingFilter </filter-name>

<Filter-class> org. springframework. web. filter. CharacterEncodingFilter </filter-class>

<Init-param>

<Param-name> encoding </param-name>

<Param-value> UTF-8 </param-value>

</Init-param>

</Filter>

<Filter-mapping>

<Filter-name> encodingFilter </filter-name>

<Servlet-name> rest </servlet-name>

</Filter-mapping>

System. out. println (t. getParameter ("username "));

You can also write the filter by yourself, but it is not recommended, because you write and use the same, there is no need for that.

I have written so much for the time being. I have recorded my learning records, and I hope you will be grateful for any problems that have not been solved!

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.