Request, response Chinese garbled problem and solving method

Source: Internet
Author: User

Request garbled refers to: the browser sent to the server requests parameter contains Chinese characters, the server gets the value of the request parameter is garbled.

Response garbled refers to: the server to the browser to send the data contains Chinese characters, the browser is displayed garbled.

The reason why the garbled is generated:
Whether it is the request garbled or response garbled, in fact, because the client (browser) and the server side of the encoding format used by the inconsistency.
Take the request garbled example: The browser sends requests to the server, because the communication between the browser and the server is essentially the socket stream, so the request parameters (characters) are converted to bytes, that is, the encoding process, the server receives the request parameters to decode (byte to character), It is then encapsulated in the request object. If the client's encoding is not unified with the server-side decoding, the value of the request parameter obtained through request is garbled.

Workaround:
One, response garbled
The data sent to the browser by the server is ISO-8859-1 encoded by default, and is displayed after the browser receives the data according to the default character set, if the default decoding character set of the browser is not

Iso-8859-1, there is garbled. For response garbled, only need to specify an encoding character set on the server side, and then notify the browser to follow this character set to decode it.

  There are three ways of doing this:
1,//Set the server-side encoding, the default is Iso-8859-1; The method must be set before Response.getwriter ()
Response.setcharacterencoding ("Utf-8");
Notifies the browser server of the data format sent
Response.setheader ("ContentType", "text/html; Charset=utf-8 ");

2,//notify the browser server to send the data format
Response.setcontenttype ("text/html; Charset=utf-8 ");

3,//Set the server-side encoding
Response.setcharacterencoding ("Utf-8");
Then use Utf-8 in the browser to decode

  This can usually be done just in case:
A, set the server-side encoding
Response.setcharacterencoding ("Utf-8");
B. Notify the browser server of the data format sent
Response.setcontenttype ("Text/html;charset=utf-8");
C, the browser uses Utf-8 to decode


Second, the request garbled
There are three ways to access from the browser: Enter the URL directly in the Address bar, click the hyperlink in the page to access, submit the form access.

The first way to access the browser by default, the parameters are encoded according to Utf-8, and the following two access methods the browser encodes the parameters according to the current page's display encoding.

So for the request garbled, only need to set the appropriate decoding format on the server side. Due to different access methods, the browser to the parameters of the encoding format is also different, in order to facilitate processing, through hyperlinks and forms of access also stipulates that the Utf-8 format must be displayed, that is, the encoding of the current page should also use Utf-8, so that the browser will uniformly use Utf-8 to encode parameters.
A, Post mode
The Post method belongs to the form submission, and the parameter exists in the request body.
Request.setcharacterencoding ("Utf-8")
B, get mode
The Get method submits the parameters to follow the URI in the request line, the server is decoded by the default iso-8859-1, there are two ways to solve garbled characters:
Method One: Modify the server-side default encoding of URI parameters
In Tomcat's server.xml, set the attribute of the element uriencoding= "UTF-8". (This property is not set by default)
For example: NOTE: 1, set the attribute of the element usebodyencodingforuri= "true", meaning that the request body and URI use the same encoding format. By setting these two properties, can not only solve the get way garbled, but also can solve the post way garbled. 2, by modifying the server.xml specified server to get and post unified according to Utf-8 decoding, requires Tomcat management of all Web applications to use UTF-8 encoding, that is, all the JSP, HTML pages use Utf-8 encoding. For example, the JSP page header information is this:

Request, response Chinese garbled problem and solving method

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.