`
liqita
  • 浏览: 287379 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Tomcat+MySql+Struts的中文问题 [过滤器]

阅读更多
开发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文件里面的参数指定)。过滤器的代码如下:

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 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>
*
* @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 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);

    }

}

编绎后把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后就可以了。
(2)对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的数据库乱码问题,很简单~~~~
分享到:
评论

相关推荐

    zebraCMS(斑马内容管理系统)1.0安装包

    windows(linux)+eclipse+mysql+mysql query browser +tomcat +zebraCMS+dreamvear+photoshop windows(linux)+netbeans+mysql+mysql query browser +tomcat +zebraCMS+dreamvear+photoshop 快速开发平台构建: ...

    zebraCMS(斑马内容管理系统)源码

    windows(linux)+eclipse+mysql+mysql query browser +tomcat +zebraCMS+dreamvear+photoshop windows(linux)+netbeans+mysql+mysql query browser +tomcat +zebraCMS+dreamvear+photoshop 快速开发平台构建: ...

    jpetstore4.0 (spring+struts+ibatis)

    用spring改造的jpetstore4.0,适用于jdk7+,tomcat6+。mysql数据库

    大学生社团互动平台改良版(ssh+ext+JFreechart+报表+mysql)

    用log4j实现日志管理,在E盘生成scstanddd.log的日志文件,用自定义的过滤器实现了权限管理。后台是Ext管理,超酷的界面。后台一个照片管理可以放大图片,超帅,前台的社团申请会在D盘jasper文件夹下生产相应的pdf...

    网上书店系统(Struts+Hibernate)(Java EE项目案例)

    (4)在web.xml 是否配置了编码过滤器。 (5)数据源配置的url(?useUnicode=true&characterEncoding=UTF-8),具体请看项目实例。 如果上面5步都没问题,你就不存在中文乱码问题。 =================================...

    公告管理系统(JSP+Struts)(Java EE项目案例)

    (4)在web.xml 是否配置了编码过滤器。 (5)数据源配置的url(?useUnicode=true&characterEncoding=UTF-8),具体请看项目实例。 如果上面5步都没问题,你就不存在中文乱码问题。 =================================...

    网上文件管理系统(Struts)(Java EE项目案例)

    (4)在web.xml 是否配置了编码过滤器。 (5)数据源配置的url(?useUnicode=true&characterEncoding=UTF-8),具体请看项目实例。 如果上面5步都没问题,你就不存在中文乱码问题。 =================================...

    毕业论文-办公自动化系统的设计和实现

    本系统采用B/S构架,以Tomcat作为服务器,基于MyEclipse开发工具,以Spring+Struts+Hibernate框架作为底层设计技术,结合MySQL数据库,通过跨平台性的JAVA语言实现逻辑功能,JSP+JAVAScript+CSS 动态网页技术进行...

    《MyEclipse 6 Java 开发中文教程》前10章

    8.7 创建Filter(过滤器) 152 8.8 创建数据库访问层(DAO) 155 8.9 修改Servlet调用后台类 158 8.10 发布,重新发布,运行和测试应用 159 8.11 调试JSP应用 160 8.12 向现有Web项目添加Web开发功能 161 8.13高级设置 ...

    基于JAVA的购物网站(毕业论文)

    3.5.3 过滤器类设计 26 3.5.4 监听器类设计 26 3.6系统的用例图 27 3.6.1 总体用例分析 27 3.6.2 用户管理用例分析 28 3.6.3 购物车管理用例分析 28 小结 30 第四章 系统的详细设计 31 4.1 数据库与POJO映射的实现 ...

    大学生互动平台1(ssh+ext+jfreechart+部分报表功能)

    页面显示采用jsp部分用struts标签,业务...还用自定义的过滤器实现后台登陆权限的实现,还改了一部分代码,既能在IE上展现,又能在Firefox下展现同时项目也可以在Weblogic下部署,我已经改好了。(原来是在tomcat下).

    基于JAVA的在线考试系统(毕业论文)

    3.5.3 过滤器类设计 19 3.5.4 监听器类设计 19 3.6系统的用例图 20 3.6.1 总体用例分析 20 3.6.2 用户管理用例分析 21 3.6.3 考试管理用例分析 21 3.6.4 考试题目管理用例分析 22 3.6.5 成绩管理用例分析 22 小结 22...

    个性化定制系统(Ajax+Spring+Hibernate)(Java EE项目案例)

    (4)在web.xml 是否配置了编码过滤器。 (5)数据源配置的url(?useUnicode=true&characterEncoding=UTF-8),具体请看项目实例。 如果上面5步都没问题,你就不存在中文乱码问题。 =================================...

    初学struts时自己做的一个简单的公告管理

    包含了简单的国际化的实现,servlet过滤器的实现,数据库jdbc连接池的使用,对数据库的简单的添加删除修改操作等. eclipse V3.2.1和myeclipse 5.1.1 GA开发的. jdk是1.5或更高。Tomcat是5.0或更高。我的mysql...

    传智播客黑马35期

    day20_过滤器 day21_监听器 day22_文件上传与下载 day23_基础加强 day24_在线支付&JavaMail; day25_编码实战day01 day26_编码实战day02 day27_编码实战day02 day28_struts2基础 day29_struts2加强 day30_...

    Java Web编程宝典-十年典藏版.pdf.part2(共2个)

    8.8.1 Struts Prepare And Execute Filter过滤器 8.8.2 struts.xml文件配置出错 8.9 精彩回顾 第9章 庖丁解牛 ——揭密Struts2高级技术 9.1 本章学习任务 9.1.1 本章知识体系 9.1.2 实例开发任务 9.2 OGNL表达式...

    JAVA程序开发大全---上半部分

    8.2.3 Struts配置文件编辑器 124 8.3 Struts应用实例:登录系统 125 8.4 本章小结 133 第9章 Hibernate框架的开发及应用 134 9.1 Hibernate框架概述 134 9.1.1 O/R Mapping(对象/关系映射)技术 134 9.1.2 ...

    java版飞机大战源码-Rockira.github.io:Rockira.github.io

    :microscope:监听器和过滤器 :ring:数据库 MySQL 索引、锁机制 事务特性、隔离级别 MySQL调优与最佳实践 :t-shirt:JDBC :ribbon:AJAX :cooked_rice:JavaWeb小项目 :hamburger:Hibernate :page_facing_up:Struts2 :...

    Java面试宝典2020修订版V1.0.1.doc

    11、过滤器有哪些作用? 35 12、JSP的常用指令? 35 13、JSP和Servlet中的请求转发分别如何实现? 35 14、JSP乱码如何解决? 36 15、session 和 application的区别? 36 16、jsp有哪些内置对象?作用分别是什么? 36 ...

Global site tag (gtag.js) - Google Analytics