First, extract two parameters in $ tomcat_home/webapps/tomcat-docs/config/http.html: uriencoding: This specifies the character encoding used to decode the URI bytes, after % XX decoding the URL. if not specified, ISO-8859-1 will be used.
Usebodyencodingforuri: This specifies if the encoding specified in contenttype shocould be used for URI query parameters, instead of using the uriencoding. this setting is present for compatibility with Tomcat 4.1.x, where the encoding specified in the contenttype, or explicitely set using request. setcharacterencoding method was also used for the parameters from the URL. the default value is false.
The preceding two Tomcat regions are set in the HTTP region of server. xml. To solve the problem of querystring Chinese garbled characters, you must set at least one of the two parameters. Set uriencoding to uriencoding = "ISO-8859-1" to specify the "ISO-8859-1" encoding so that the character encoding of querystring is the same as that of post body. Usebodyencodingforuri is compatible with Tomcat 4.x. The value is set to "true" or "false", which means "do you want querystring and post body to be encoded in the same byte? ", If set to true, it can also meet the" ISO-8859-1 "encoding needs. We recommend that you use the uriencoding settings. After all, usebodyencodingforuri is used to ensure compatibility with Tomcat 4.x. However, if according to the original text, theoretically these two shards are not set, Tomcat should also use the "ISO-8859-1" encoding, then why is there a problem? Because of the querystring program bug in Tomcat source code, you must add the urlencoding parameter to server. xml. Example of setting ctor:
<Connector
debug="0"
acceptCount="100"
connectionTimeout="20000"
disableUploadTimeout="true"
port="80"
redirectPort="8443"
enableLookups="false"
minSpareThreads="25"
maxSpareThreads="75"
maxThreads="150"
maxPostSize="0"
URIEncoding="ISO-8859-1"
>
</Connector>
// Note, after my experiment, set uriencoding = "ISO-8859-1" after the get method is still garbled, but change it to uriencoding = "gb2312.
In this case, I will do some additional charge... in general, when we use Tomcat 4 to retrieve data through get or post, we usually use the filter method to solve the problem of Chinese parameter data. However, after Tomcat 5.0.19, when resolving the number of Chinese characters, you must consider using get or post. The two solutions are different. If you use the get Method for instant messaging, as described in the article by the senior master, or use string name1 = new string (request. getparameter ("name ")). getbytes ("ISO-8859-1"), "gb2312"); if the POST method is used, it is extended to use the uniform system to solve the request in Chinese. setcharacterencoding ("big5"); but at the beginning, my last practice was to use the filter method: first, judge whether the callback method uses the get or POST method. If the get method is used, the callback uses the first code. If the POST method is used, use the second code.
In addition, you can use filter to solve post:
1. Implement a filter. Set the processing character set to GBK. (There is a complete example in the webapps/servlet-examples directory of Tomcat. See the configuration of Web. xml and setcharacterencodingfilter .)
First, add <% @ page contenttype = "text/html; charset = gb2312" %> On the JSP page.
1) Just Put % tomcat installation directory %/webapps/Servlets-examples/WEB-INF/classes/filters/setcharacterencodingfilter. copy the class file to your webapp directory/filters. If there is no filters directory, create one.
2) Add the following lines to your web. xml:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>