Request, response Chinese garbled problem and solving method

Source: Internet
Author: User

the request garbled refers to the: The request parameter that the browser sends to the server contains Chinese characters, and the value of the request parameter obtained by the server is garbled; response garbled refers to the: The server sends the data to the browser contains Chinese characters, the browser is garbled, the reason for garbled: whether it is the request garbled or response garbled, in fact, because the client (browser) and the server side of the encoding format inconsistent caused. 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. Solve: One, response garbledThe data that the server sends to the browser by default is ISO-8859-1 encoded, after the browser receives the data according to the default character set to decode after displays, if the browser default decoding character set is not iso-8859-1, 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: 1, A, set the server-side encoding response.setcharacterencoding ("Utf-8");The default is Iso-8859-1; The method must be set before Response.getwriter () B, notifying the browser server of the data format sent Response.setheader ("ContentType", "text/html; Charset=utf-8 ");2, A, notify the browser server sent data format Response.setcontenttype ("Text/html;charset=utf-8");Equivalent to Response.setheader ("ContentType", "text/html; Charset=utf-8 "), it will actually overwrite the response.setcharacterencoding (" Utf-8 "), in the development only need to set B, set the server-side encoding Response.setcontenttype ("Text/html;charset=utf-8");3, A, set the server-side encoding response.setcharacterencoding ("Utf-8");B, the browser uses Utf-8 to decodeSummary: Set: 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 decodeThis is how I set it up, just in case; second, the request garbledThere 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 post method belongs to the form submission, the parameters exist in the request body. request.setcharacterencoding ("Utf-8")B, the get way to submit parameters will 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: Modify the server-side default encoding of URI parameters in Tomcat's server.xml, set the element's properties 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.