Effective Solution to Chinese garbled characters in Get and Post requests

Source: Internet
Author: User

Effective Solution to Chinese garbled characters in Get and Post requests

This article mainly describes how to effectively solve the garbled problem in web requests. In fact, there are many solutions, and different request types have different solutions.

This article only describes the most convenient one:

1. First modify the encoding method in jsp

2. If garbled characters occur in requests submitted in Post mode, you can set the encoding format each time when the request parses the data:

 

3. Set the encoding in the URL

4. Set database Properties

5. Finally, modify the settings in the table design.

 

You can also use the encoding filter. The most common method is to use the encoding filter provided by spring:

Add the following configuration in Web. xml (note that its position must be the first execution filter ):

<Filter>
<Filter-name> charsetFilter </filter-name>
<Filter-class> org. springframework. web. filter. CharacterEncodingFilter </filter-class>
<Init-param>
<Param-name> encoding </param-name>
<Param-value> UTF-8 </param-value>
</Init-param>
<Init-param>
<Param-name> forceEncoding </param-name>
<Param-value> true </param-value>
</Init-param>
</Filter>

What this filter needs to do is to set the encoding format for all requests and responses:

Request. setCharacterEncoding ("UTF-8 ");
Response. setCharacterEncoding ("UTF-8 ");

2. For the garbled Get method, because the parameters are passed through the URL, the encoding format set through the request above does not work, at this time, you can encode the URL before each request occurs: for example, Location. href = "/encodeURI" ("http: // localhost/test/s? Name = Chinese & sex = female ");

Of course, there is also a simpler method, that is, configuring the URL encoding format on the server side:

Modify the tomcat configuration file server. xml:
<Connector URIEncoding = "UTF-8"
Port = "8080" maxHttpHeaderSize = "8192"
MaxThreads = "150" minSpareThreads = "25" maxSpareThreads = "75"
ConnectionTimeout = "20000" disableUploadTimeout = "true"/>

Just add URIEncoding = "UTF-8" and restart tomcat.

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.