初学JSP,开发系统时遇到了一些中文乱码问题
我上网找了许多方法都不能根本解决问题。
jsp页面都写了<%@ page contentType="text/html;charset=gb2312" language="java" %>,
数据库里的字符类型是NVACHAR
不用filter过滤时,存入到数据库中是正常的中文,JSP页面显示出来也是正常中文,但数据从表单传递到Struts那时成乱码了,问号了。
用了filter后,表单数据传递到struts里是正常中文,但存储到数据库中成了乱码。
为什么了??

解决方案 »

  1.   

    package filter;import java.io.BufferedWriter;
    import java.io.FileWriter;
    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;
    /**
     * @author Dave
     */
    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);
            BufferedWriter  bw = new BufferedWriter(new FileWriter("/temp/insertlog.log"));;
            //bw.
            //response.getOutputStream().println();    }    /** 
        * 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);    }}//-----------------------------------------------------------
    //web.xml <filter>
    <filter-name>SetCharacterEncoding</filter-name>
    <filter-class>filter.SetCharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>GB2312</param-value>
    </init-param>
    <init-param>
    <param-name>ignore</param-name>
    <param-value>true</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>SetCharacterEncoding</filter-name>
    <servlet-name>Action Servlet</servlet-name>
    </filter-mapping>
    <filter-mapping>
    <filter-name>SetCharacterEncoding</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
    <filter-mapping>
    <filter-name>SetCharacterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
      

  2.   

    工具myeclipse,struts1.2,hibernate3.0,sql server2000
      

  3.   

    response对象也要设置一下字符编码
    response.setCharacterEncoding(encoding); 
      

  4.   

    用这个通用方法吧,很好使的。
    public static final String toUnicode(String str)
    {
    try
            {
          return new String(str.getBytes("iso-8859-1"),"gb2312");
    }
            catch(Exception e)
            {
          return str;
    }
    }
      

  5.   

    还是无效,
    我发现页面传递到struts的ACTION时,中文字码是ISO-8859-1,Struts显示不出正常中文,但数据库接受后,能在库中正常显示
    从库中传出来也是ISO-8859-1的编码,页面也能正常显示,但我用过滤器后,FILTER把中文数据转成GBK编码,
    struts能正常显示,接着它把这GBK编码的中文字存入库后就成了乱码。
    所以有没有这种过滤方法,从页面提交到Struts时过滤成GBK,对数据操作完后,struts把数据存入库前,过滤为ISO-8859-1呢?
      

  6.   

    中文不能总转来转去的,这样转就转不回来了,一般只要jsp、tomcat(容器)设置下就行了,不需所有的东西都加中文转换,你试试。
      

  7.   

    保存到数据之前要转换编码格式为iso-8859-1,取出后再转换gb2312,可以通过两个方法来实现。