Resolving garbled issues when transferring data between a Web browser and a servlet

Source: Internet
Author: User

1, using GetParam and other methods to get request parameters encountered garbled

The encoding used by the request parameter that the browser sends is the encoding that is used when the Web page is opened. If the server side obtains the request parameter which sends over, by default uses the Iso8859-1 to decode the operation, the Chinese must have the garbled problem.

The data submitted by the Post and get methods are differentiated:

For data submitted by post, request.setcharacterencoding ("gb2312"), which explicitly specifies the use of encoding when fetching request parameters. Note that only the Post method is valid.

For Get mode, you can only manually decode string newName = new String (Name.getbytes ("iso8859-1"), "gb2312"), encode and decode again, this method is also valid for post. In Tomcat's

The uriencoding that can be configured in Server.xml can specify the encoding that the server uses by default when it obtains the request parameters, and then once and for all refuse to get the garbled problem of the request parameter. You can also specify

The Usebodyencodingforuri parameter, so that request.setcharacterencoding also works with the Get mode request, but because the publishing environment generally does not allow the modification of this property, try not to use it. Get mode is more complex to handle.

So the JSP uses post as much as possible to submit the data.

2. Garbled when using request to output data to client

The browser uses the system default platform bytecode to open the data sent by the server, if the server side uses non-platform code to output character byte data, it is necessary to explicitly specify the code table used by the browser encoding to prevent garbled problems.

Response.AddHeader ("Content-type", "text/html;charset=gb2312"); Response.getwriter (). Write ("Chinese"); output data, which is a stream of characters, Response this character will be transferred after transcoding operation

Out to the browser, this process by default using the Iso8859-1 code table, and iso8859-1 in the absence of Chinese, so the transcoding process in lieu of Chinese, resulting in garbled problems. You can specify the target code table used by the response during transcoding to prevent confusion

Code. Response.setcharcterencoding ("gb2312"), in fact response also provides a setcontenttype ("text/html;charset=gb2312") method, more convenient.

Resolving garbled issues when transferring data between a Web browser and a servlet

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.