When developing Web applications, No matter what framework technology is used for development, an exception of Chinese garbled characters or mismatched encoding methods is required when accessing data involving Chinese characters from a database, this evening, I finally fixed the Chinese problem of Tomcat + MySQL + Struts, and used a very simple method to solve it quickly.
Before doing the following, all the charsets of HTML/JSP are set to charset = gb2312.
The first thing to solve is the form submission garbled. When using the actionform provided by struts, no matter whether the form uses the struts tag or HTML Tag, you can use GET/set of actionform to obtain and set the element values of the form. the getparameter () method is the same, but the extracted data is garbled without processing. The main reason is 1. tomcat's J2EE implementation adopts the default iso8859_1 to process the processing parameters for form submission, that is, post method submission. tomcat uses a different processing method than the POST method for requests submitted by the get method in query-string processing. Therefore, if you want to correctly display and obtain Chinese data, use the following solutions: (1) for the form submitted by the POST method, write a filter (filer, the filter is called before the data submitted by the user is processed. You can use this Java code to change the parameter encoding method (the target encoding method can be passed through the Web. parameters in the XML file are specified ). The filter code is as follows:
Import java. Io. ioexception;
Import javax. servlet. filter;
Import javax. servlet. filterchain;
Import javax. servlet. filterconfig;
Import javax. servlet. servletexception;
Import javax. servlet. servletrequest;
Import javax. servlet. servletresponse;
Import javax. servlet. unavailableexception;
/**
* <P> example filter that sets the character encoding to be used in parsing
* Incoming request, either unconditionally or only if the client did not
* Specify a character encoding. configuration of this filter is based on
* The following initialization parameters: </P>
* <Ul>
* <Li> <strong> encoding </strong>-the character encoding to be configured
* For this request, either conditionally or unconditionally based on
* The <code> ignore </code> initialization parameter. This Parameter
* Is required, so there is no default. </LI>
* <Li> <strong> ignore </strong>-if set to "true", any character encoding
* Specified by the client is ignored, and the value returned by
* <Code> selectencoding () </code> method is set. If set to "false,
* <Code> selectencoding () </code> is called <strong> only </strong> If
* Client has not already specified an encoding. By default, this
* Parameter is set to "true". </LI>
* </Ul>
*
* <P> although this filter can be used unchanged, it is also easy
* Subclass it and make the <code> selectencoding () </code> method more
* Intelligent about what encoding to choose, based on characteristics
* The incoming request (such as the values of the <code> Accept-language </code>
* And <code> User-Agent </code> headers, or a value stashed in the current
* User's session. </P>
*
* @ Author Craig Mcclanahan
* @ Version $ revision: 1.2 $ Date: 2004/03/18 16:40:33 $
*/
Public class setcharacterencodingfilter implements filter {
// --------------------------------------------------------- Instance variables
/**
* The default character encoding to set for requests that pass through
* This filter.
*/
Protected string encoding = NULL;
/**
* The filter configuration object we are associated with. If this value
* Is null, this filter instance is not currently configured.
*/
Protected filterconfig = NULL;
/**
* Shoshould a character encoding specified by the client be ignored?
*/
Protected Boolean ignore = true;
// ----------------------------------------------------------- Public methods
/**
* Take this filter out of service.
*/
Public void destroy (){
This. Encoding = NULL;
This. filterconfig = NULL;
}
/**
* Select and set (if specified) the character encoding TO BE USED
* Interpret Request Parameters for this request.
*
* @ Param request the Servlet request we are processing
* @ Param result the servlet response we are creating
* @ Param chain the filter chain we are processing
*
* @ Exception ioexception if an input/output error occurs
* @ Exception servletexception if a servlet error occurs
*/
Public void dofilter (servletrequest request, servletresponse response,
Filterchain chain)
Throws ioexception, servletexception {
// Conditionally select and set the character encoding to be used
If (ignore | (request. getcharacterencoding () = NULL )){
String encoding = selectencoding (request );
If (encoding! = NULL)
Request. setcharacterencoding (encoding );
}
// Pass control on to the next Filter
Chain. dofilter (request, response );
}
/**
* Place this filter into service.
*
* @ Param filterconfig the filter configuration object
*/
Public void Init (filterconfig) throws servletexception {
This. filterconfig = filterconfig;
This. Encoding = filterconfig. getinitparameter ("encoding ");
String value = filterconfig. getinitparameter ("Ignore ");
If (value = NULL)
This. Ignore = true;
Else if (value. inclusignorecase ("true "))
This. Ignore = true;
Else if (value. inclusignorecase ("yes "))
This. Ignore = true;
Else
This. Ignore = false;
}
// ---------------------------------------------------------- Protected Methods
/**
* Select an appropriate character encoding to be used, based on
* Characteristics of the current request and/or filter Initialization
* Parameters. If no character encoding shoshould be set, return
* <Code> null </code>.
* <P>
* The default implementation unconditionally returns the value configured
* By the <strong> encoding </strong> initialization parameter for this
* Filter.
*
* @ Param request the Servlet request we are processing
*/
Protected string selectencoding (servletrequest request ){
Return (this. Encoding );
}
}
Compile the class file in the classes directory and add the following code to the Web. xml file of the Web application:
<Filter>
<Filter-Name> set character encoding </filter-Name>
<Filter-class> com. Neusoft. Equipment. Controller. 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> Any Character Set supporting multi-byte encoding, such as gb2312, GBK, and utf8, can store Chinese characters. Of course, the number of Chinese Characters in gb2312 is much smaller than that in gb2312, GBK and so on can all be encoded in utf8. The destination encoding method is GBK. You can restart tomcat.
(2) For the form submitted by the get method, because the parameter is followed by the user's URL request, Tomcat's processing method is different from the POST method. Therefore, the filter set above has no effect on the get method and needs to be set elsewhere. Find the server of route. in the xml configuration file, locate the Settings section of the connector component of 80 (or 8080, which is modified by yourself) and add an attribute to the connector component: uriencoding = "GBK ". The modified connector component is as follows:
<! -- Define a non-ssl http/1.1 Connector on port 80 -->
<Connector Port = "80" maxhttpheadersize = "8192"
Maxthreads = "150" minsparethreads = "25" maxsparethreads = "75"
Enablelookups = "false" redirectport = "8443" acceptcount = "100"
Connectiontimeout = "20000" disableuploadtimeout = "true" uriencoding = "GBK"/> after modification, restart tomcat to correctly process the form data submitted by the get method.
The second solution is garbled data during database access. Different databases often support different codes, which leads to chaotic application time. Different databases often have different solutions. For MySQL, there are also various solutions on the Internet, but I personally think those are too complicated. Now there is an extremely simple solution: Modify the MySQL configuration file, open the root directory after MySQL installation, and find my. in the init file, modify the following statement in the [mysqld] area: default-character-set = Latin1 to default-character-set = GBK, and then add it in the [client] area: default-character-set = GBK. After modification, remember to do one thing. Go to the service program under the management tool of the widows control panel and restart the MySQL service, this fundamentally solves the problem of garbled MySQL databases ~~~~
From: http://dev.csdn.net/author/lin_bei/66de56da81df44ee9ac379099a0600cf.html