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

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

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?

     */

&n

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

相关文章:

你感兴趣的文章:

标签云: