代码
<%@ page language="java" contentType="text/html; charset=GBK"%>  
<%   
    //request.setCharacterEncoding("GBK");   
    String contentId = request.getParameter("contentId");   
%>  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=GBK">  
<title>Jsp Test</title>  
</head>  
<body>  
<form method = "post" action="ActionVerify.jsp">  
<table>  
    <tr>  
        <td>  
            <input name = "contentId" type = "text" value= "<%=contentId %>">  
        </td>  
        <td>  
            <input name = "submit" type = "submit" value= "提交">  
        </td>  
    </tr>  
</table>  
</form>  
</body>  
</html>  
  
<%@ page language="java" contentType="text/html; charset=GBK"%>  
<%   
//request.setCharacterEncoding("GBK");   
%>  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=GBK">  
<title>Insert title here</title>  
</head>  
<body>  
<form action="">  
    <table>  
        <tr>  
            <td>  
                <%=request.getParameter("contentId") %>  
            </td>  
        </tr>  
    </table>  
</form>  
  
</body>  
</html>  
我已经设置了编码方式为GBK,但是我设置了request.setCharacterEncoding("GBK");就可以正常显示,<%@ page language="java" contentType="text/html; charset=GBK"%>不能起到当前页面全局作用的效果吗? 
第一个为index.jsp第二个为Actionverify.jsp

解决方案 »

  1.   

    1、将所有jsp页面编码方式统一,(我设置的是"GBK",)然后在jsp页面中加两句话:<%@ page contentType="text/html;charset=GBK" language="java"   pageEncoding="GBK" %><META http-equiv=Content-Type content="text/html; charset=GBK">2、将tomcat安装目录下conf文件夹下的server.xml 里加入字符编码方式:URIEncoding="GBK" <Connector 
    port="8080"                maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
                    enableLookups="false" redirectPort="8443" acceptCount="100"
                    debug="0" connectionTimeout="20000" 
                    disableUploadTimeout="true" URIEncoding="GBK" />3、设置编码转换过滤器,写一个设置字符的类,并在web.xml配置该文件。一.filter类的代码 :package com.yourpackage;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;/** *//**
     * Example filter that sets the character encoding to be used in parsing the
     * incoming request
     * 字符集过滤器,解决Java中文问题,非常有效
     */
    public class SetCharacterEncodingFilter implements Filter ...{    /** *//**
           * Take this filter out of service.
         */
        public void destroy() ...{
          }
        /** *//**
           * Select and set (if specified) the character encoding to be used to
           * interpret request parameters for this request.
         */
        public void doFilter(ServletRequest request, ServletResponse response,
          FilterChain chain)throws IOException, ServletException ...{      request.setCharacterEncoding("GB2312");    // 传递控制到下一个过滤器
          chain.doFilter(request, response);
          }    public void init(FilterConfig filterConfig) throws ServletException ...{
          }
    }二.web.xml里面的配置:<filter>
        <filter-name>Set Character Encoding</filter-name>
        <filter-class>netstore.framework.util.SetCharacterEncodingFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>Set Character Encoding</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>4、如果以上方法都不行,检查开发环境是否有问题,我之前就是因为用的eclipse+lomboz整合版导致中文乱码和一些jsp页面编译后会出现少"\"的错误,换成myeclipse就OK了!
      

  2.   

    JSP就是会出现这种情况,还有像什么读数据库里的值它也回出先乱码,这很正常。
    像你说的这个,其实<%@ page language="java" contentType="text/html; charset=GBK"%>
    确实是起全局作用,就是说整个页面的编码方式是GBK,但是对从别的页面传过来的值就可能不起作用。你必须再转换一次编码方式,一般这种情况只会出现在传中文的时候,你自己写的那是一种解决方法,还可以这样转换:(request.getParameter("contentId").getBytes("ISO8859_1"),"GBK")
      

  3.   

    为什么要通过iso-8859-1来转化呢?如果别的页面使用utf-8传来的,我使用难道传输的时候使用iso-8859-1吗
      

  4.   

    <%=new String(request.getParameter("contentId").getBytes("ISO-8859-1"),"GBK") %>
      

  5.   

    <%=new String(request.getParameter("contentId").getBytes("ISO-8859-1"),"GBK") %>or<%=new String(request.getParameter("contentId").getBytes("ISO-8859-1"),"gb2312") %>