Servlet garbled Problem Solution

Source: Internet
Author: User

The following analysis is based on the following assumptions:Unified encoding format using UTF-8

POST method:

Server:
In the first call request. when getparameter is used, parameter is parsed. If the encoding used for parsing (Decoding) is not set, the default encoding org is used. apache. coyote. constants. default_character_encoding ("ISO-8859-1") resolves the parameter. Therefore, if the parameter contains Chinese characters, garbled characters are parsed. Therefore, to support Chinese parsing, you must set the encoding format used for parsing parameters before calling request. getparameter for the first time, such:
Request. setcharacterencoding ("UTF-8 ");
Client:
Similarly, to enable the browser to support Chinese display on the client, you must set the webpage encoding format, for example:
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">

 

Get method:

Client:

If the URI contains Chinese characters, it must be encoded:

<A href = "download? Filename = <% = java.net. urlencoder. encode (file, "UTF-8") %> "> <% = file %> </a>

Utf8 encoding is used here.

Server:

The server must know the encoding method used by the client and then decode it in the same way. You can tell the Server Client encoding method in server. xml:

<Connector connectiontimeout = "20000" Port = "8080" protocol = "HTTP/1.1"
Redirectport = "8443"Uriencoding = "UTF-8 "/>

<Connector Port = "8009" protocol = "AJP/1.3" redirectport = "8443"Uriencoding = "UTF-8 "/>

After the get method in form is submitted, the browser uses the encoding format of the current page to encode the Chinese parameter Uri, so human encoding is not required. Make sure the page encoding is utf8: <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">

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.