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.