Servlet-Chinese Garbled text, servlet-Garbled text
Background
Starting from Tomcat5.x, The GET and POST Methods submit information. Tomcat uses different encoding methods.
For GET requests, Tomcat does not consider using the encoding set by request. setCharacterEncoding ("UTF-8"), and will always use the ISO-8859-1 encoding.
For POST requests, Tomcat uses the encoding set for request. setCharacterEncoding ("UTF-8"), and uses "ISO-8859-1" if not set ".
1. get method, that is, garbled Request Parameters
Cause:
The HTTP ctor configuration in The official Tomcat documentation, which describes The URIEncoding attribute:
This specifies the character encoding used to decode the URI bytes, after % xx decoding the URL. If not specified, the ISO-8859-1 will be used.
If not specified, the URI is decoded using a ISO-8859-1.
There are two solutions:
1. Root method: Modify the server. xml file
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8" />
2 local method: Get a parameter, decode it, and encode it again.
String name=request.getParameter(parameter);byte[] arr=name.getBytes("ISO8859-1");String nameAfterTransfer=new String(arr,"UTF-8");
2 post method, that is, the request body garbled
Cause: if no Request is set, use ISO-8859-1 decoding, regardless of the encoding method of your submitted page.
Solution:
The Container Provided Filters configuration in Tomcat official documentation. The system provides some Filters. One of them is org. apache. catalina. filters. SetCharacterEncodingFilter, which, as the name suggests, is a set encoding filter. There are two attributes: encoding, the encoding name, and ignore. Determine whether any character encoding specified by the user proxy is ignored. If this attribute is true, the user generation is ignored.
Is any value provided by the browser. If false, the encoding is set only when the user agent does not specify an encoding. The default value is false.
<Filter> <filter-name> SetCharacterEncodingFilter </filter-name> <filter-class> org. apache. catalina. filters. setCharacterEncodingFilter </filter-class> <init-param> <param-name> encoding </param-name> <param-value> Self-set encoding </param-value> </init-param> <param-name> ignore </param-name> <param-value> true </param-value> </init-param> </filter> <filter-mapping> <filter-name> SetCharacterEncodingFilter </filter-name> <url-pattern>/* </url-pattern> </filter-mapping>
The implementation in the SetCharacterEncodingFilter class is to set request. setCharacterEncoding (the parameter configured above );
Conclusion:
We usually request. setCharacterEncoding ("UTF-8") settings, just change the request body encoding method.
3. get and post encoding: Use the useBodyEncodingForURI attribute.
UseBodyEncodingForURI. If the value is true, the URI is encoded using the Request body.
The official Tomcat Documentation explains the useBodyEncodingForURI attribute in this way,
If the character encoding of the request is unknown (not provided by the browser, not by setcharacterencodingfilter or a filter similar to the request. setcharacterencoding method), the default encoding is "ISO-8859-1 ". The URIEncoding setting has no effect on this.
4 do you guess the following results will be garbled?
In the following html page operations, the page has been UTF-8 encoding, POST submitted to the background:
<! DOCTYPE html>
The answer is: Garbled. Are you correct?
Conclusion:
1 Tomcat ignores the encoding method of the submitted page, or the request does not contain any encoding method.
2 If request. setCharacterEncoding ("UTF-8") is not set or a filter is added, Tomcat will still use "ISO-8859-1" decoding.
3. Therefore, no matter what encoding format is on the page before submission, we must set our own encoding method.
Ultimate Solution
If we want to decode all using UTF-8, the ultimate solution is,
1. Set useBodyEncodingForURI = "true" in server. xml"
2 set request. setCharacterEncoding ("UTF-8 ");// Adding a filter in essence is also handled in this way, so it is not said to add a filter.
In this way, we achieve the get and post request all UTF-8 decoding effect.
Finally, if something is wrong, you are welcome to correct my summary.
If you want to wear a crown, you must bear it.