[转帖]Struts+Mysql+Tomcat5.0.28+mysql-connector-java-3.0.16-ga-bin_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
5
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3833 | 回复: 4   主题: [转帖]Struts+Mysql+Tomcat5.0.28+mysql-connector-java-3.0.16-ga-bin        下一篇 
yd7563337
注册用户
等级:少将
经验:10191
发帖:128
精华:1
注册:2011-8-9
状态:离线
发送短消息息给yd7563337 加好友    发送短消息息给yd7563337 发消息
发表于: IP:您无权察看 2011-8-15 11:05:20 | [全部帖] [楼主帖] 楼主

国际化的东西带来的问题还真的好多,各国语言不同,所使用的字符集都不一样,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
      * 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 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.equalsIgnoreCase("true"))
            this.ignore = true;
            else if (value.equalsIgnoreCase("yes"))
            this.ignore = true;
            else
            this.ignore = false;
      }
      // ------------------------------------------------------ Protected Methods
      /**
      * Select an appropriate character encoding to be used, based on the
      * characteristics of the current request and/or filter initialization
      * parameters. If no character encoding should 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文件:将下面的代码放在WEB.xml文件的<display-name>下面就行了
<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>




赞(0)    操作        顶端 
Michelle
注册用户
等级:列兵
经验:88
发帖:58
精华:0
注册:2011-8-11
状态:离线
发送短消息息给Michelle 加好友    发送短消息息给Michelle 发消息
发表于: IP:您无权察看 2011-8-19 14:17:21 | [全部帖] [楼主帖] 2  楼

MessageDbMessageDbMessageDbMessageDb



赞(0)    操作        顶端 
Michelle
注册用户
等级:列兵
经验:88
发帖:58
精华:0
注册:2011-8-11
状态:离线
发送短消息息给Michelle 加好友    发送短消息息给Michelle 发消息
发表于: IP:您无权察看 2011-8-19 14:29:55 | [全部帖] [楼主帖] 3  楼

你好,呵呵!



赞(0)    操作        顶端 
Michelle
注册用户
等级:列兵
经验:88
发帖:58
精华:0
注册:2011-8-11
状态:离线
发送短消息息给Michelle 加好友    发送短消息息给Michelle 发消息
发表于: IP:您无权察看 2011-8-19 14:31:30 | [全部帖] [楼主帖] 4  楼

你好吗??



赞(0)    操作        顶端 
Michelle
注册用户
等级:列兵
经验:88
发帖:58
精华:0
注册:2011-8-11
状态:离线
发送短消息息给Michelle 加好友    发送短消息息给Michelle 发消息
发表于: IP:您无权察看 2011-8-19 14:33:49 | [全部帖] [楼主帖] 5  楼



赞(0)    操作        顶端 
总帖数
5
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论