Format differences between request.setcharacterencoding () and Request.setcontenttype ("Html/css;charset")

Source: Internet
Author: User
Tags tomcat server

1, request.setcharacterencoding () is the setting of the value taken from the request or the value taken from the database

Once specified, the correct string can be obtained directly through GetParameter (), and if not specified, the ISO8859-1 encoding is used by default. It is important to note that no getparameter () can be performed until setcharacterencoding () is executed. Furthermore, the designation is valid only for the Post method and not for the Get method. The reason for the analysis is that when the first getparameter () is executed, Java will parse all the submissions according to the encoding, and the subsequent getparameter () is no longer parsed, so setcharacterencoding () is invalid. In the case of a Get method submission form, the content submitted in the URL, from the beginning has been parsed according to the content of the submission, setcharacterencoding () naturally invalid.

Get needs to be in Tomcat's server.xml:

uriencoding= "GBK"/>) add uriencoding= "GBK" to solve the GET request garbled problem

2, Response.setcontenttype ("TEXT/XML;CHARSET=GBK") is in the Settings page for Chinese encoding
The former is to set the dynamic text (parameters, database), the latter set the page static text

RESPONSE.SETCONTENTTYPE Specifies the encoding of the HTTP response and specifies the encoding that the browser displays.
Response.setcharacterencoding sets the encoding of the HTTP response, if the encoding format was previously set using Response.setcontenttype, Overwrites the previous setting with the encoding format specified by response.setcharacterencoding.
As with Response.setcontenttype, calling this method must precede getwriter execution or before response is committed .


Add:
Issue 1: Passing Parameters from JSP page page to servlet displaying garbled characters
Workaround:

If the Post method commits, add the following code:
Request.setcharacterencoding ("GBK");//note first of all to ensure that the code one, JSP page with what coding here with what code (GBK/GB2312, etc.), this is only valid for the Post submission method
Response.setcontenttype ("TEXT/XML;CHARSET=GBK") is the Chinese encoding in the settings page
Note: The above two lines of code must be placed in Printwriterout=response.getwriter (); Before
Get method Commit
If the variable name is used
String name = new String (Request.getparameter ("name"). GetBytes ("Iso-8859-1"), "GBK");

Issue 2: Resolve Get method Commit garbled problem:

Modify the Tomcat Server.xml file, plus the red part of the code

<connector port= "8080" protocol= "http/1.1"
connectiontimeout= "20000"
Redirectport= "8443"
uriencoding= "GBK"/>

Note: The above character encoding format according to their own needs to set, as long as the consistency can

Format differences between request.setcharacterencoding () and Request.setcontenttype ("Html/css;charset")

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.