随笔-211  评论-26  文章-8  trackbacks-0
在使用JSP的过程中,最使人头疼的一个问题就是中文乱码问题,以下是我在软件开发中遇到的乱码问题以及解决方法。

  1、JSP页面乱码

  这种乱码的原因是应为没有在页面里指定使用的字符集编码,解决方法:只要在页面开始地方用下面代码指定字符集编码即可,

  2、数据库乱码

  这种乱码会使你插入数据库的中文变成乱码,或者读出显示时也是乱码,解决方法如下:
  在数据库连接字符串中加入编码字符集
  String Url="jdbc:sql&ct=0' target=_blank>mysql://localhost/digitgulf?user=root&password=root&useUnicode=true&characterEncoding=GB2312";
  并在页面中使用如下代码:
  response.setContentType("text/html;charset=gb2312");
  request.setCharacterEncoding("gb2312");

  3、中文作为参数传递乱码

  当我们把一段中文字符作为参数传递个另一页面时,也会出现乱码情况,解决方法如下:
  在参数传递时对参数编码,比如
  RearshRes.jsp?keywords=" + java.net.URLEncoder.encode(keywords)
  然后在接收参数页面使用如下语句接收
  keywords=new String(request.getParameter("keywords").getBytes("8859_1"));

  4、JSP页面乱码加这句 

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="err.jsp" %>

 

/**  
        *   ISO-LATIN-1编码转换为字节编码,并去掉右面的空格  
        *   <BR>但是在NULL的场合,返回空字符串。  
        *   @param             String     输入的字符串  
        *   @return         String     变换后输出的字符串  
        */  
      public   String   n2u(String   s)   {  
          String   r="";  
          int   keta;  
          if(s   ==   null)   {  
              return   "";  
          }  
          try   {  
              r   =   new   String(s.getBytes("ISO8859_1"),   "gb2312");  
              keta   =   r.length()   -   1;  
              for(int   i   =   r.length()   -   1;   i   >=   0;   i--)   {  
                  if(!r.substring(i,   i   +   1).equals("   ")){  
                      break;  
                  }  
                  keta   =   i   -   1;  
              }  
              r   =   r.substring(0,   keta   +   1);  
          }   catch   (IOException   e)   {  
              e.printStackTrace();  
          }  
          return     r;  
      }  
   
   
  如何这个函数所在的类,实例化后的对象是cc  
  则在取数据时  
  xinghao=request.getParameter("xinghao");  
  该为  
  xinghao=cc.n2u(request.getParameter("xinghao"));  

 

在用struts框架时,由于表单提交的时候先提交到Action再转发到自建的Action的子类来处理所以用在servlet里设置request.setCharacterEncoding("GBK");的方法不能解决中文问题,因为表单提交到Action的时候已经乱码了。

   解决的方法是建立一个Filter,代码在tomcat的example里可以找到。

public class SetCharacterEncodingFilter implements Filter{
 protected String encoding = null;
 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 {

     // 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);
        }
 
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>servfilter</filter-name>
        <filter-class>filters.SetCharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>GBK</param-value>
      </init-param>
    </filter>
  
    <filter-mapping>
        <filter-name>servfilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

posted on 2006-10-03 17:05 dragon 阅读(68) 评论(0)  编辑  收藏 所属分类: jsp mysql 乱码解决方案系列