我已经配置了过滤器在其中使用request.setCharacterEncoding("gbk");并且所有页面都使用gbk编码,但表单提交后还是会乱码.除非使用getBytes("ISO-8859-1")这样才可以,不过好麻烦啊;怎么配置才能不这样啊

解决方案 »

  1.   

    你的过滤器没写好吧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 response 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
         * @return String
         */
        protected String selectEncoding(ServletRequest request) {        return (this.encoding);    }
    }
    web.xml<filter>
            <filter-name>Encoding</filter-name>
            <filter-class>framework.filter.SetCharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>GB2312</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>Encoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
      

  2.   

    我没有动态设置,而是直接确定了编码方式
    web.xml<filter>
      <filter-name>Two</filter-name>
      <filter-class>com.xndag.filter.Two</filter-class>
    </filter>
    <filter-mapping>
      <filter-name>Two</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
    在com.xndag.filter.Two中直接用了
    request.setCharacterEncoding("gbk");
      

  3.   

    好郁闷啊,弄了一天了还没弄好,把.do换成.jsp,直接在jsp页面使用request.setCharacterEncoding("gbk");还是不行啊,麻烦对乱码比较有经验的给提下思路吧,快郁闷死了
      

  4.   

    getBytes会不会有点慢啊,它是什么原理呢?是一个个取出来重新构造字符串吗?如果这样要有几千个字节的字符串会不会影响速度啊
      

  5.   

    如果通过socket的getInputStream()得到请求的输入流
    如何正确的格式化???
      

  6.   

    另外一个问题
    就是说客户端socket的方法getInputStream()会得到一个InputStream
    一般要格式化为字符串
    如果网页中提交的参数有中文的,那么就会乱码
    这样的话就不太好处理
    用UTF-8格式化也没用
      

  7.   

    在过滤器里加上 response.setCharacterEncoding(encoding); 再试试看。
      

  8.   

    我靠,NND终于让我搞定了,弄了半天是因为我的表单是以get方式提交的
      

  9.   

    那我如果想通过get也就是url传中文参数怎么弄