jsp中文乱码终极解决方法

作者: ldsea 分类: 程序生活 发布时间: 2008-05-30 15:26

一 找出问题的根源
    乱码可能出现的地方:1 jsp页面中
                        2 jsp页面之间相互传参的参数
                        3 与数据库中数据的存取
    基本归纳为以上几种。

二 寻找解决方案
    1 出现在jsp页面中,是由于没有设置jsp页面的中文字符编码。
    2 出现在jsp页面之间相互传参,是由于参数没有设置正确的字符编码。
    3 以上2个问题解决了,那么存到数据库中,自然就不存在乱码。除非你对存入到数据库里的数据再次进行编码。
三解决方法:
1的解决方法
[code]<% @ page contentType = " text/html;charset=gb2312 " %> [/code]
加上这句解决jsp页面中的中文乱码显示,tomcat编译完后向客户端输出的html文件不是采

用中文编码,所以会导致乱码产生。

2的解决方法
2.1
[code]<% request.setCharacterEncoding( " gb2312 " ); %> [/code]
加上这句解决jsp页面中的中文参数传递乱码。
因为浏览器默认使用的编码是“UTF-8”发送请求参数。
我们把它改为”gb2312″就ok了。

2.2 [code]String(request.getParameter("name").getBytes("ISO8859_1"),"GB2312");[/code]
这句的意思是,把传来的参数全部编码转换成gb2312,这样做的缺点是每次传来一个参数

都要这样写,很麻烦。

同样可通过设置server.xml配置文件来实现。

[code]< Connector
port ="8080"                 maxHttpHeaderSize ="8192"
                maxThreads ="150" minSpareThreads ="25" maxSpareThreads ="75"
                enableLookups ="false" redirectPort ="8443" acceptCount ="100"
                connectionTimeout ="20000" disableUploadTimeout ="true"   URIEncoding ="gb2312" /> [/code]
但是这样就应用到整个webapp中去了。

另:
[code]<% @page pageEncoding = " gb2312 " %>[/code]
此句是为了让jsp编译器能正确地解码含有中文字符的jsp页面。

其它方法还可以修改web.xml文件,配置一个过滤器。其原理都一样,只是换种方式而已。
有的书上专门写了一个函数来解决乱码,实际上对比一下就知道那种解决方法的好与坏。
回过头来一看,解决乱码也不过如此。
ok,实际就加上这3句搞定问题。

[code]<% @page pageEncoding = " gb2312 " %>
<% @ page contentType = " text/html;charset=gb2312 " %>
<% request.setCharacterEncoding( " gb2312 " ); %> [/code]
请大家有什么想法,以及有什么不对的地方请各位说明,写在留言上,大家一起讨论。

补充:添加一个过滤器SetCharacterEncodingFilter.java
[code]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 = null;

  protected FilterConfig filterConfig = null;

  public void init(FilterConfig filterConfig) throws ServletException {
    this.filterConfig = filterConfig;
    this.encoding = filterConfig.getInitParameter("encoding");
  }

  public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain) throws IOException, ServletException {
    request.setCharacterEncoding("UTF-8");
    chain.doFilter(request, response);
  }

  public void destroy() {
    this.encoding = null;
    this.filterConfig = null;
  }

  protected String selectEncoding(ServletRequest request) {
    return (this.encoding);
  }
}[/code]
在web.xml添加
  
[code]
    <!– 过滤器 –>
    <filter>
        <filter-name>Set Character Encoding</filter-name>
        <filter-class>cn.ldsea.base.SetCharacterEncodingFilter</filter-class>
        <init-param>
             <param-name>encoding</param-name>
             <param-value>UTF-8</param-value>
        </init-param>
     </filter>

     <filter-mapping>
        <filter-name>Set Character Encoding</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>  
[/code]

发表回复

您的电子邮箱地址不会被公开。