Struts + MySQL + tomcat5.0.28 + mysql-connector-java-3.0.16-ga-bin.jar International garbled Solution

Source: Internet
Author: User
There are still many problems brought about by internationalization. Different Languages and character sets are used differently. The character sets used for Java, MySQL, tomcat, and browsers are different, I have been talking about struts, MySQL, and tomcat several times over the past few days. All of them have been written by myself. They use a unified encoding and character set. Unfortunately, they are not capable enough, the problem still needs to be solved. After checking on the Internet for a long time and practicing it for many days, the problem was finally solved.
Because we need to take into account the languages of many countries, the project was first established internationally. After the connection method was changed from the struts connection pool to the Tomcat connection pool, the garbled characters I encountered were as follows:
1. garbled characters read from resource files on the page;
2. garbled characters read by the database
3. garbled characters written in the database
4. In actionform verification, garbled characters returned by errors are not passed, that is, request and IE parameters are passed.
5. There is a JS write time option to jump out of the box, if there is an error in charset = UTF-8, But if I change to charset = GBK or gb2312, everything is normal, it's strange that I still cannot find the answer.
Wow, a lot of garbled characters .... The headers are big for several days.
The following is my solution
1. garbled characters read from the resource file on the page: this is the easiest solution. Use native2ascii-encoding gb2312 applicationresources. properties under the doc to write the applicationresources. properties file.
The applicationresources_zh_cn.properties command is used for character encoding conversion. After converting the original Chinese character to unicode encoding, the simplified Chinese character can be obtained. The traditional Chinese character also uses the same command, but you can change bg2312 to BGK.
2. Database read/write garbled characters. At the beginning, they were affected by the previous SQL SERVER + JDBC statements (at that time, writing data to the database does not need to be done, only GBK = new string (ISO. getbytes ("ISO-8859-1"), "GBK") conversion on the line) I am also reading and writing are in the conversion, so complicated and very troublesome, then the connection code JDBC: mysql: // localhost: 3306/database in the connection pool? Autoreconnect = true & amp; useunicode = true & amp; characterencoding = GBK with an & amp; useunicode = true & amp; characterencoding = GBK, this ensures that the unified encoding character set is used during database operations and solves two garbled characters.
4. The request and response garbled characters have been found on the internet for a long time. There are also two solutions for conversion. There is also a way to write a filter. I chose the latter, this method uses two files: Filter and web. XML file with the code at the end.
5. As for JS, there is no way to solve this problem. You have to change it to <% @ page contenttype = "text/html; charset = GBK" %> On the JSP page, it's okay.
So far, the garbled problem has finally come to an end. It feels pretty good. After so long, I am so happy that I am so happy.

Bytes -------------------------------------------------------------------------------------------------------------------
Filter code: encodingfilter. Java

Package com .***.***.***;

Import javax. servlet .*;
Import java. Io. ioexception;

/**
* <P> 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>
*
*/
Public class encodingfilter 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 );

}

} // EOC

Web. xml file: Put the following code below <display-Name> of the web. xml file.
<Filter>
<Filter-Name> encodingfilter </filter-Name>
<Display-Name> encodingfilter </display-Name>
<Description> set the Request Encoding </description>
<Filter-class> com. VoIP. admin. util. encodingfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> gb18030 </param-value>
</Init-param>
<Init-param>
<Param-Name> ignore </param-Name>
<Param-value> true </param-value>
</Init-param>
</Filter>

<Filter-mapping>
<Filter-Name> encodingfilter </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.