日期:2014-05-18  浏览次数:20764 次

征集jsp/servlet乱码解决方案,最好能够有个终极解决办法
本人分别用过 asp, asp.net 和 java 开发web应用程序,但发现java别的地方都好,就是乱码问题实在让人头大:

1、在一台机器上运行得好好的程序,拷贝工程到另一台机器上运行时就变成了乱码
2、本来一个好好的html或jsp页面,如果是从一个 servlet 中 forward 过来,就变成了乱码
3、硬编码的一条插入语句,存到数据库中去却变成了乱码
......

不同的问题要用不同的办法解决


实在想不明白 tomcat 中为什么会有那么多种编码,很多配置文件是用 UTF-8,但有些配置文件却用 ISO-8859-1。Unicode 被应用已经不止10年了,为什么象 ISO-8859-1 这类编码还在tomcat中被一直使用,害得中国的java程序员一直被编码问题蹂躏。人家 asp.net 的乱码问题就很少,有时出现一点,差不多在一个配置文件设置一次就可以全部搞掂,为什么 tomcat 不可以呢。



------解决方案--------------------
一般分为两种情况 1:提交表单接收中文一般需要在接收的servlet里设置 request.setCharacterEncoding("GBK");
2:<a href="aaa?userName='张三'"> 这种形式传的需要在接收的servlet里做如下操作
String userName=request.getParameter("userName");
byte[] temp=userName.getBytes("ISO-8859-1");
userName=new String(temp,"GBK");
------解决方案--------------------
像楼主的问题不难,一般通用的解决方案是加一个过滤器的话就OK了..

过滤器代码如下:
Java code

package com.lanzhengwu.filters;

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 Administrator
 *
 * 更改所生成类型注释的模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
public class SetCharacterEncodingFilter implements Filter {

    /**
     * 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 == n