My summary of Chinese garbled solution

Source: Internet
Author: User
Tags remove filter tomcat server

Foreword: The garbled question, has been causing our headache the question. I'm free to do some research today. Don't go too deep, just know the fur. Consider not comprehensive enough, only to give you a little help enough.

A brief introduction of the difference between Pageencoding and contenttype (excerpted from the Internet)

Pageencoding: Sets the character set encoding in the JSP source file and response body.
ContentType: Sets the character set encoding and MIME type of the JSP source file and response body.

If the pageencoding attribute exists, then the character encoding of the JSP page is determined by the pageencoding,
Otherwise, it is determined by the charset in the ContentType attribute, and if CharSet does not exist, the character encoding of the JSP page is based on the default iso-8859-1.

Second, the Environment test drill

POST request

1, Conditions: HTML or JSP charset:text/html;charset=gb2312 (that is, browser page encoding method)
No filter,
Server.xml not set
For example Request.getparamter, "Wang" converted into íônew String ("Wang". GetBytes ("gb2312"), "iso-8859-1")
Conclusion: This is because the "Wang" encoded by GB2312 is encoded as íô by Iso-8859-1.
Therefore, you need to convert the new String ("Íô". GetBytes ("Iso-8859-1"), "gb2312");

2, conditions: On the basis of 1, server.xml increase
uriencoding= "GBK" usebodyencodingforuri= "true" does not work.
The value is modified several times and has no effect on the results in 1.
Conclusion: It is inferred that this setting does not work on post requests.
3, conditions: On the basis of 1, increase fiter, request.setcharacterencoding ("gb2312");
Then Request.getparameter ("name"), get the normal value directly.
Conclusion: Request.setcharacterencoding needs to be set before Request.getparameter ("name"). Refer to the English document.

4, Conditions: On the basis of 3, modify request.setcharacterencoding ("Utf-8");
Then, the Chinese character "Wang" will not normally parse out, because "Wang" first after gb2312--->iso-8859-1---->utf-8 coding
If it is reversed, it cannot be reversed. 锟 a new string (Name.getbytes ("Utf-8"), "Iso-8859-1"). GetBytes ("Iso-8859-1"), "gb2312");
Conclusion: therefore do not set two or more than two different encodings, resulting in a complex coding, may be irreversible.


GET Request:
1, Conditions: JSP pageencoding= "UTF-8" Charset=utf-8 ", Filter request.setcharacterencoding (" Utf-8 ");
<a href= "<%=request.getcontextpath ()%>/login.do?method=validate&username= Wang &password=121322" " > Testing </a>

Analysis: Wang----utf-8----iso-8859-1----request.setcharacterencoding ("utf-8") does not work--
Therefore, string Name=new string (Request.getparameter ("username"). GetBytes ("Iso-8859-1"), "Utf-8", to display normal Chinese.

2, the browser address bar manual Tapping links
So Wang---GBK---iso-8859-1
So string Name=new string (Request.getparameter ("username"). GetBytes ("Iso-8859-1"), "GBK");
3, on the basis of 1,
3.1) Remove Filter filters
3.1.1) only uriencoding= "UTF-8"
"Wang"------utf-8----iso-8859-1---through tomcat server.xml---utf-8
Therefore Request.getparameter ("username") is normal Chinese.
3.1.2) Only usebodyencodingforuri= "true"
With 3.1.3
3.1.3) server.xml Add uriencoding= "UTF-8" usebodyencodingforuri= "true"
Wang----utf-8----iso-8859-1
So string Name=new string (Request.getparameter ("username"). GetBytes ("Iso-8859-1"), "Utf-8"); For normal Chinese
3.2) Add filter request.setcharacterencoding ("Utf-8")

3.2.1) Only uriencoding= "UTF-8"
"Wang"------utf-8----iso-8859-1---through tomcat server.xml---utf-8, note that filter does not work
Therefore Request.getparameter ("username") is normal Chinese.
3.2.2) Only usebodyencodingforuri= "true"
"Wang"------utf-8----iso-8859-1---filter---utf-8
Therefore Request.getparameter ("username") is normal Chinese. That same 3.1.1
3.2.3) server.xml Add uriencoding= "UTF-8" usebodyencodingforuri= "true"
Ditto

4, on the basis of 2
4.1) Remove Filter Fiter
4.1.1) only uriencoding= "UTF-8"
Wang------GBK-----iso-8859-1---through tomcat server.xml---utf-8
Request.getparameter ("username"); Garbled. If the reverse coding is irreversible,
New String (Request.getparameter ("username"). GetBytes ("Utf-8"), "Iso-8859-1"). GetBytes ("Iso-8859-1"), " GBK ")----锟 Catty
4.1.2) Only usebodyencodingforuri= "true"
With 4.1.3
4.1.3) server.xml Add uriencoding= "UTF-8" usebodyencodingforuri= "true"
Wang------GBK-----iso-8859-1
So Name=new String (Request.getparameter ("username"). GetBytes ("Iso-8859-1"), "GBK");

4.2) Adding filter request.setcharacterencoding ("Utf-8");
4.2.1) only uriencoding= "UTF-8"
Wang------GBK-----iso-8859-1---through tomcat server.xml---utf-8
Request.getparameter ("username"); Garbled. If the reverse coding is irreversible,
New String (Request.getparameter ("username"). GetBytes ("Utf-8"), "Iso-8859-1"). GetBytes ("Iso-8859-1"), " GBK ")----锟 Catty
4.2.2) Only usebodyencodingforuri= "true"
With 4.1.1
4.2.3) server.xml Add uriencoding= "UTF-8" usebodyencodingforuri= "true"
Ditto
5, Server.xml increase uriencoding= "UTF-8" usebodyencodingforuri= "true", regardless of setting a filter, modify the uriencoding= "UTF-8" code many times.
No effect on the result, Usebodyencodingforuri priority is higher than uriencoding.

Summarize:
1) request.setcharacterencoding only for post requests, get requests (usebodyencodingforuri= "TRUE" configured in Server.xml).
2 server.xml Increase uriencoding= "UTF-8" usebodyencodingforuri= "true" only works on get requests.
3 manual link to the address bar. GBK----iso-8859-1 based on system code
4) request.setcharacterencoding should be set before Request.getparameter.
5) for Server.xml only uriencoding= "UTF-8" configuration. The GET request URI is UTF-8 encoded
6 if usebodyencodingforuri= "true" priority is higher than uriencoding= "UTF-8". That is, after setting the former, the latter does not work.

Therefore, it is recommended that the best JSP pageencoding,contenttype,server.xml uriencoding= "UTF-8" usebodyencodingforuri= "true" Fileter for request.setcharacterencoding, consistent.

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.