The difference between Response.setcontenttype and request.setcharacterencoding

Source: Internet
Author: User

the difference between Response.setcontenttype and request.setcharacterencoding

Request.setcharacterencoding () is a value that sets the value taken from the request or fetched from the database
Response.setcontenttype ("TEXT/XML;CHARSET=GBK") is the set page for the Chinese encoding
The former is set 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, this method must be invoked before getwriter execution or before response is committed

Add:
Issue 1: Pass parameters from JSP page page to servlet display Chinese garbled
Solution:

If it is a POST method submission, add the following code:
Request.setcharacterencoding ("GBK");/note first to ensure that the encoding one to, the JSP page with what encoding the encoding (gbk/gb2312, etc.), this code is only valid for post submission
Response.setcontenttype ("TEXT/XML;CHARSET=GBK") is the set page for the Chinese encoding
Note: The above two lines of code must be placed in Printwriterout=response.getwriter (); Before
Get method Commit
such as variable name with
String name = new String (request.getparameter ("name"). GetBytes ("Iso-8859-1"), "GBK");

Problem 2: Resolve Get method Submit garbled problem:

Modify the Tomcat Server.xml file, plus the red part 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 consistent can



Reprint Address: http://blog.csdn.net/u012188794/article/details/41695827

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.