jsp向jsp传递中文出现乱码,怎么解决啊

解决方案 »

  1.   

    第一步:写个fiter类SetCharacterEncodingFilter.javapackage com.niexiong.fiter;
    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;
    public class SetCharacterEncodingFilter implements Filter
    {        protected String encoding = "GB2312";
            protected FilterConfig filterConfig = null;
            protected boolean ignore = true;
            public void destroy()
            {
                    this.encoding = null;
                    this.filterConfig = null;
            }
            public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException
            {                if (!ignore)
                    {
                            String encoding = selectEncoding(request);
                            request.setCharacterEncoding(encoding);
                    }
                    chain.doFilter(request, response);        }        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 String selectEncoding(ServletRequest request)
            {                return (this.encoding);        }
    }第二步:web.xml中添加  <filter>
    <filter-name>SetCharacterEncoding</filter-name>
    <filter-class>com.niexiong.fiter.SetCharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>GBK</param-value>
    </init-param>
    <init-param>
    <param-name>ignore</param-name>
    <param-value>false</param-value>
    </init-param>
    </filter>
        
    <filter-mapping>
    <filter-name>SetCharacterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping> 
    第三步:jsp页面都添加<%@ page contentType="text/html;charset=GBK" %>
      

  2.   

    在jsp页面加上
    <% request.setCharacterEncoding("GBK");%>另外保存jsp时,注意转换类型
      

  3.   

    1楼说的类,编译通不过,出错:软件包javax.servlet不存在,还有很多其他问题,是怎么回事?
      

  4.   

    没有建web工程。我只是想把中文能够正常显示而已
      

  5.   

    <%@ page contentType="text/html;charset=gbk"%>  <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
    <title></title>  </head> 
      

  6.   

    有的时候你会发现即使2楼和8楼的方法都用了,还是会出现乱码的.
    当出现这样的问题的时候,你可以使用这样的方法:
    <%out.println(new String(temp.getBytes("iso-8859-1")));%>
    temp就是字符串的变量名.