1. Encoding Configuration
<connector acceptcount= "100″connectiontimeout=" 20000″disableuploadtimeout= "true" enablelookups= "false" Maxhttpheadersize= "8192″maxsparethreads=" 75″maxthreads= "150″minsparethreads=" 25″port= "80″redirectport=" 8443″ uriencoding= "GBK" usebodyencodingforuri= "true" />
URLs are encoded using GBK.
uriencoding : used to set the encoding used for content passed through the URI, Tomcat encodes the content that the client transmits using the encoding specified here. The parameters we submit via the Get method are actually submitted by URI, managed by this parameter, and if this parameter is not set, Tomcat will use the default iso8859-1 to encode the contents of the client.
Usebodyencodingforuri : using the same code as the Body to handle the URI, this setting is to maintain compatibility with the TOMCAT4, the original TOMCAT4 and TOMCAT5 Squadron parameter processing is not the same, in TOMCAT4 get and post encoding is the same, so The issue of Get and post can be solved by setting the request.setcharacterencoding once in the filter. However, in Tomcat5, get and post processing is carried out separately, the get processing through the previous uriencoding processing, the post content is still through request.setcharacterencoding processing, in order to maintain compatibility, With this setting.
So, set uriencoding to solve the parameter problem in get, configure the filter to solve the post parameter problem, or set Usebodyencodingforuri to true , both get and post use filters to solve parameter problems.
2, the page encoding can set the request header: In the head to add
<meta http-equiv= "Content-type" content= "text/html; CHARSET=GBK "/>
3. Modify the browser configuration
IE: Configuration of the option "Always Send URLs in Utf-8" in Internet Options/advanced;
Firefox: Enter About:config in the Address bar and modify Network.standard-url.escape-utf8 to False (default is true);
If you want the browser to Url-encode directly into Utf-8, modify Network.standard-url.encode-utf8 to True (the default is False).
4. You may also have the following configuration when using spring
Primarily for garbled problems that occur when interacting with a database, such as inserting Chinese records or reading Chinese data.
<property name= "Driverclassname" >
<value>com.mysql.jdbc.Driver</value>
</property>
<property name= "url" >
<value>jdbc:mysql://localhost:3306/newfang?useUnicode=true& CHARACTERENCODING=GBK&autoReconnect=true</value>
</property>
5, the code to solve garbled problems
is mainly used to solve the problem of individual garbled, such as the display of the Web page in Chinese is normal except for one or two statements, then the garbled problem for these one or two statements can be used by this method.
QString = new String (qString. GetBytes ("Iso8859_1″"), "GBK");
or qstring = Urlencoder.encode (qString, "GBK");//java url encoding method
5 , Advanced, Web. XML configuration, adding Filter Filter Filters
Used to deal with the whole station garbled problem, in fact, is mainly used when the action and JSP pages to interact with.
<!– is used to solve Chinese garbled problem –>
<filter>
<filter-name>set Character encoding</filter-name>
<filter-class>com.qa.util.SetEncodingFilter</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>
Setencodingfilter Code:
PackageCom.qa.util;Importjavax.servlet.*;ImportJava.io.*; Public classSetencodingfilterImplementsfilter{protectedString encoding=NULL;//defines the default character encoding methodprotected BooleanIgnore=true;//defines whether the client-specified encoding should be ignoredprotectedFilterconfig filterconfig=NULL;//defines the filter configuration object, which, if NULL, indicates that the filter is not configured Public voidDestroy ()//Stop the work of the filter{ This. encoding=NULL; This. filterconfig=NULL;}//Set character encoding Public voiddoFilter (servletrequest req,servletresponse res,filterchain chain)throwsioexception,servletexception{if(ignore| | (req.getcharacterencoding () = =NULL) {req.setcharacterencoding (selectencoding (req));} Chain.dofilter (req,res);}//Start Filter Public voidInit (Filterconfig filterconfig)throwsservletexception{ This. filterconfig=Filterconfig; This. encoding=filterconfig.getinitparameter ("encoding"); String value=filterconfig.getinitparameter ("Ignore");if(value==NULL) This. ignore=true;Else if(Value.equalsignorecase ("true")|| Value.equalsignorecase ("yes")) This. ignore=true;Else This. ignore=false;}//Select the appropriate character encoding methodprotectedString selectencoding (servletrequest req) {return This. Encoding;}//returns the Filterconfig object Publicfilterconfig Getfilterconfig () {returnFilterconfig;}//set Filterconfig Object Public voidsetfilterconfig (Filterconfig filterconfig) { This. filterconfig=filterconfig;}}
6, the server Apache on the garbled.
In addition to the above situation, there are Apache configuration problems, the following points are noted:
1) conf/httpd.conf
Change Adddefaultcharset iso-8859-1 to Adddefaultcharset GBK
2) Apache has been rewrite
The Chinese parameter in the URL that needs to be rewrite is encoded two times (encode), because Apache will do a URL decoding at rewrite, when JK carries forward the request, it will not be the encoded string anymore;
Or, when receiving the request, first use iso-8859-1 to take the byte stream, and then use UFT-8 to new String. (New String (Str.getbytes ("Iso-8859-1″)," Uft-8″))
Http://blog.sina.com.cn/s/blog_6310009d01014v9d.html
Tomcat and Web. XML configuration