查看完整版本: [-- Tomcat+MySql+Struts中文问题绝妙的解决方案 --]

风信Java论坛 ›› J2EE/Java Web 讨论交流中心 ›› Tomcat+MySql+Struts中文问题绝妙的解决方案 登录 -> 注册

1F Tomcat+MySql+Struts中文问题绝妙的解决方案   唧唧 Post by : 2008-06-11 11:09:04.0

      开发Web应用程序时,无论是用什么样的框架技术来开发,一碰从数据库存取涉及到中文的数据,就要面对中文乱码或者是各种编码方式不匹配的异常,今天晚上终于搞定了Tomcat+MySql+Struts的中文问题,用到了很简单的方法,很快就能搞定。

      在做以下工作之前,所有的HTML/JSP的charset都设为charset=gb2312。

      第一个要解决的是表单提交乱码问题。在使用Struts提供的ActionForm过程中,无论表单采用的是Struts标签还是Html标签,都可以用ActionForm的Get/Set来获取和设置表单的元素值(它们的作用效果与request.getParameter()方法一样),但提取出来的数据不经过处理的话就是乱码,主要的原因是1.Tomcat的J2EE实现对表单提交即Post方法提交时,处理参数采用默认的ISO8859_1来处理2.Tomcat对Get方法提交的请求在query-string处理时采用了和Post方法不一样的处理方式。所以如果要正确地显示和获取中文数据采用的解决方案:(1)对于Post方法提交的表单通过编写一个过滤器(filer)的方法解决,过滤器在用户提交的数据被处理之前被调用,可以通过这个Java代码改变参数的编码方式(目标编码方式可以通过Web.xml文件里面的参数指定)。过滤器的代码如下:

java代码
  1. import java.io.IOException;  
  2. import javax.servlet.Filter;  
  3. import javax.servlet.FilterChain;  
  4. import javax.servlet.FilterConfig;  
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.ServletRequest;  
  7. import javax.servlet.ServletResponse;  
  8. import javax.servlet.UnavailableException;  
  9.  
  10. /**  
  11.  * <p>Example filter that sets the character encoding to be used in parsing the  
  12.  * incoming request, either unconditionally or only if the client did not  
  13.  * specify a character encoding.  Configuration of this filter is based on  
  14.  * the following initialization parameters:</p>  
  15.  * <ul>  
  16.  * <li><strong>encoding</strong> - The character encoding to be configured  
  17.  *     for this request, either conditionally or unconditionally based on  
  18.  *     the <code>ignore</code> initialization parameter.  This parameter  
  19.  *     is required, so there is no default.</li>  
  20.  * <li><strong>ignore</strong> - If set to "true", any character encoding  
  21.  *     specified by the client is ignored, and the value returned by the  
  22.  *     <code>selectEncoding()</code> method is set.  If set to "false,  
  23.  *     <code>selectEncoding()</code> is called <strong>only</strong> if the  
  24.  *     client has not already specified an encoding.  By default, this  
  25.  *     parameter is set to "true".</li>  
  26.  * </ul>  
  27.  *  
  28.  * <p>Although this filter can be used unchanged, it is also easy to  
  29.  * subclass it and make the <code>selectEncoding()</code> method more  
  30.  * intelligent about what encoding to choose, based on characteristics of  
  31.  * the incoming request (such as the values of the <code>Accept-Language</code>  
  32.  * and <code>User-Agent</code> headers, or a value stashed in the current  
  33.  * user's session.</p>  
  34.  *  
  35.  * @author Craig McClanahan  
  36.  * @version $Revision: 1.2 $ $Date: 2004/03/18 16:40:33 $  
  37.  */ 
  38.  
  39. public class SetCharacterEncodingFilter implements Filter {  
  40.  
  41.     // ----------------------------------------------------- Instance Variables  
  42.     /**  
  43.      * The default character encoding to set for requests that pass through  
  44.      * this filter.  
  45.      */ 
  46.     protected String encoding = null;  
  47.  
  48.  
  49.     /**  
  50.      * The filter configuration object we are associated with.  If this value  
  51.      * is null, this filter instance is not currently configured.  
  52.      */ 
  53.     protected FilterConfig filterConfig = null;  
  54.  
  55.  
  56.     /**  
  57.      * Should a character encoding specified by the client be ignored?  
  58.      */ 
  59.     protected boolean ignore = true;  
  60.  
  61.     // --------------------------------------------------------- Public Methods  
  62.     /**  
  63.      * Take this filter out of service.  
  64.      */ 
  65.     public void destroy() {  
  66.         this.encoding = null;  
  67.         this.filterConfig = null;  
  68.  
  69.     }  
  70.  
  71.     /**  
  72.      * Select and set (if specified) the character encoding to be used to  
  73.      * interpret request parameters for this request.  
  74.      *  
  75.      * @param request The servlet request we are processing  
  76.      * @param result The servlet response we are creating  
  77.      * @param chain The filter chain we are processing  
  78.      *  
  79.      * @exception IOException if an input/output error occurs  
  80.      * @exception ServletException if a servlet error occurs  
  81.      */ 
  82.     public void doFilter(ServletRequest request, ServletResponse response,  
  83.                          FilterChain chain)throws IOException, ServletException {  
  84.         // Conditionally select and set the character encoding to be used  
  85.         if (ignore || (request.getCharacterEncoding() == null)) {  
  86.             String encoding = selectEncoding(request);  
  87.             if (encoding != null)  
  88.                 request.setCharacterEncoding(encoding);  
  89.         }  
  90.         // Pass control on to the next filter  
  91.         chain.doFilter(request, response);  
  92.     }  
  93.  
  94.  
  95.     /**  
  96.      * Place this filter into service.  
  97.      *  
  98.      * @param filterConfig The filter configuration object  
  99.      */ 
  100.     public void init(FilterConfig filterConfig) throws ServletException {  
  101.         this.filterConfig = filterConfig;  
  102.         this.encoding = filterConfig.getInitParameter("encoding");  
  103.         String value = filterConfig.getInitParameter("ignore");  
  104.         if (value == null)  
  105.             this.ignore = true;  
  106.         else if (value.equalsIgnoreCase("true"))  
  107.             this.ignore = true;  
  108.         else if (value.equalsIgnoreCase("yes"))  
  109.             this.ignore = true;  
  110.         else 
  111.             this.ignore = false;  
  112.     }  
  113.  
  114.     // ------------------------------------------------------ Protected Methods  
  115.     /**  
  116.      * Select an appropriate character encoding to be used, based on the  
  117.      * characteristics of the current request and/or filter initialization  
  118.      * parameters.  If no character encoding should be set, return  
  119.      * <code>null</code>.  
  120.      * <p>  
  121.      * The default implementation unconditionally returns the value configured  
  122.      * by the <strong>encoding</strong> initialization parameter for this  
  123.      * filter.  
  124.      *  
  125.      * @param request The servlet request we are processing  
  126.      */ 
  127.     protected String selectEncoding(ServletRequest request) {  
  128.         return (this.encoding);  
  129.     }  
  130.  

编绎后把class文件放在classes目录下,并在Web应用的web.xml文件中添加如下代码:

<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>

      只要是gb2312,gbk,utf8等支持多字节编码的字符集都可以储存汉字,当然,gb2312中的汉字数量远少于gbk,而gb2312,gbk等都可在utf8下编码,这里指定目标编码方式是gbk,重新启动Tomcat后就可以了。

      对Get方法提交的表单,由于参数是紧跟在用户的URL请求后面,Tomcat对其的处理方法与Post方法不一样。所以上面设置的过滤器对Get方法没有作用,它需要在其他地方设置。找到Tomca的server.xml配置文件,找到对80(或者是8080等别的,这个是自己修改后的)的Connector组件的设置部分,给这个Connector组件添加一个属性:URIEncoding="GBK"。修改后的Connector组件是这样的:

 <!-- 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"/>这样修改后,重启Tomcat就可以正确处理GET方法提交的表单数据了。

      第二个要解决的是数据库存取数据出现的乱码等情况。对于不同的数据库往往支持不同的编码,造成了应用时比较混乱,不同的数据库的解决方法往往是不同的,针对MySql,网上也有各种各样的解决方案,但个人觉得那些太繁了,现在有一个极其简单的解决办法:修改MySql的配置文件,打开MySql安装后的根目录,找到my.init文件,把[mysqld]区的如下语句:default-character-set=latin1修改为:default-character-set=gbk,然后在[client]区增加:default-character-set=gbk,修改后记得做一件事情,到Widows控制面板的管理工具下的服务程序,把Mysql服务停止了重新启动,这样就根本解决了MySql的数据库乱码问题,很简单~~~~


风信Java论坛 ›› J2EE/Java Web 讨论交流中心 ›› Tomcat+MySql+Struts中文问题绝妙的解决方案 登录 -> 注册

查看完整版本: [-- Tomcat+MySql+Struts中文问题绝妙的解决方案 --]
CopyRight © 2008-2009 JavaWind.Net Studio All Rights Reserved
Powered By JWind.BBS Vesion 1.0.0 Beta1 Processed in 10 ms,0 (Queries)  Gzip enabled
粤ICP备07511478号