Struts Mysql Tomcat5.0.28 mysql-connector-jav…

2008-02-23 10:14:48来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

国际化的东西带来的问题还真的好多,各国语言不同,所使用的字符集都不一样,Java,MySQL,Tomcat,浏览器等等用的字符集也不一样,这几天气得我都说了好几次不用什么Struts,Mysql,Tomcat了,全部都是自己写出来好了,用统一的编码统一的字符集,可惜能力不够,说说而已,问题还是得解决。在网上查了好久,自己也实践了好多天,问题终于算是解决了。
因为要考虑到很多国的语言,一开始就把项目立足于国际化,在把连接方式从Struts连接池改为Tomcat的连接池之后,原来我遇到的乱码有
1、资源文件里读出来在页面上的乱码;
2、数据库读出来的乱码
3、数据库写进去的乱码
4、在ActionForm验证不通过Errors返回的乱码,也就是request,IE参数传递的乱码了。
5、有一个JS写的时间选择跳出框,如果在charset=UTF-8的时候就出错,但如果我改为charset=GBK或GB2312时就一切正常了,很奇怪的现象,我还找不出答案来。
哇,好多的乱码。。。。头都大了好几天。
下面是我的解决方法
1、资源文件里读出来在页面上的乱码:这个最容易解决了,把写好的ApplicationResources.properties文件,在DOC底下用 native2ascii -encoding gb2312 ApplicationResources.properties
ApplicationResources_zh_CN.properties 命令来个字符编码转换,将原来的中文转为Unicode编码就搞定了中文简体,繁体也用同样的命令,只是把 bg2312 改为 bgk 就可以了。
2、数据库读写的乱码,刚开始的时候因为受以前的SQL Server JDBC 影响(在那时写入数据库是可以不用做什么工作的,只是在读出来的时候来个 gbk = new String(iso.getBytes("ISO-8859-1"), "GBK") 转换就行了)我也都在把读写都在转换,搞得好复杂也很麻烦,后来在连接池连接代码jdbc:mysql://localhost:3306/database?autoReconnect=true&useUnicode=true&characterEncoding=GBK那里加上一个&useUnicode=true&characterEncoding=GBK,保证了在数据库操作时候使用了统一的编码字符集,又解决了两个乱码问题,一举两得,嘿嘿。
4、request,response的乱码在网上找了好久,也有两个解决的办法,也是来个转换,还有种办法是写一个过滤器,我选择了后者,因为简单:),这方法用到两个文件,一个是 filter ,一个是 Web.XML 文件,代码在后面。
5、至于JS的这个问题,还没办法,只好在JSP页面上改为<%@ page contentType=“text/html; charset=GBK“%>了,反正这样也没问题。
到此为止,乱码问题总算告一段落了,感觉蛮不错的,郁闷了这么久,终于可以高兴了好大一段子了。

-------------------------------------------------------------------------------------------------------------------
过滤器使用代码:EncodingFilter.java

package com.***.***.***;

import javax.Servlet.*;
import java.io.IOException;

/**
* <p>Filter that sets the character encoding to be used in parsing the
* 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 the
* <code>selectEncoding()</code> method is set. If set to "false,
* <code>selectEncoding()</code> is called <strong>only</strong> if the
* 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 to
* subclass it and make the <code>selectEncoding()</code> method more
* intelligent about what encoding to choose, based on characteristics of
* 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 filterConfig = null;


/**
* Should 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 to

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:JBoss3.x和4.x下配SqlServer JDBC驱动

下一篇:Log4j源代码阅读—Log4j中主要的类