设置一个过滤器,
web.xml加
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>com.hotpepper.util.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter><filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>public class SetCharacterEncodingFilter
    implements Filter {  // ----------------------------------------------------- Instance Variables
  /**
   * 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;  // --------------------------------------------------------- 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 {    
    String encoding = selectEncoding(request);    if (encoding != null) {
      request.setCharacterEncoding(encoding);
    } else {
    request.setCharacterEncoding(encoding);
    }
    response.setContentType("text/html;charset=UTF-8");
    // 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");  }  // ------------------------------------------------------ Protected Methods
  /**
   * Select an appropriate character encoding to be used, based on the
   * characteristics of the current request and/or filter initialization
   * parameters.  If no character encoding should be set, return
   * <code>null</code>.
   * <p>
   * The default implementation unconditionally returns the value configured
   * by the <strong>encoding</strong> initialization parameter for this
   * filter.
   *
   * @param request The servlet request we are processing
   */
  protected String selectEncoding(ServletRequest request) {    return (this.encoding);  }}

解决方案 »

  1.   

    你最好所有的编码都是用utf-8,这样的话,你将不会出现乱码!!!!
      

  2.   

    mysql是什么版本的,我用的是5.0版,再mysql server instance configuration wizard中设置字符集为gbk就就行了.
      

  3.   

    我用的是MYSQL5.0版的啊,也是GBK!
    UTF-8?是在哪里改?是在页面还是在配置文件里改??
    谢谢各们啊!
      

  4.   

    把所有的编码格式都改成GBK就好了,数据库,jsp,
      

  5.   

    我花了好长时间才把所有文件里的GB2312改成了GBK,但运行之后还是老样子:四个功能块有两个是好的,原来的还是乱码!我用的是MYSQL4.1版的,用的驱动是:mysql-connector-java-3.2.0-alpha-bin.jar不知道对不对啊?
      

  6.   

    网上也有人这么说:
        GB2312是GBK的子集,GBK是GB18030的子集,GBK是包括中日韩字符的大字符集合,如果是中文的网站 推荐GB2312, GBK有时还是有点问题.为了避免所有乱码问题,应该采用UTF-8,将来要支持国际化也非常方便 ,UTF-8可以看作是大字符集,它包含了大部分文字的编码。 
        使用UTF-8的一个好处是其他地区的用户(如香港台湾)无需安装简体中文支持就能正常观看你的文字而不会出现乱码
      

  7.   

    一般都是采用utf-8,都改过来看看。。数据库也改!
      

  8.   

    在mysql的配置文件ini配置文件,里面搜下encoding,改成utf-8就可以了
      

  9.   

    在连接Mysql的时侯,可以指定编码,JDBC的URL可以像下面这样写
    jdbc:mysql://127.0.0.1:3306/NK_DB?useUnicode=true&characterEncoding=utf-8&useOldAliasMetadataBehavior=true
    其中NK_DB是数据库名称,useUnicode表示是使用自定义编码,utf-8是编码方式,一般中文建议使用UTF-8,用不是gbk或gb2312,GBK和gb2312都为中文简体编码
      

  10.   

    数据库编码要和页面的编码一致。
    而且要分你是get方式传递参数,还是post方式。
      

  11.   

     写一个过滤器,在web.xml里面配置过滤器
      

  12.   

    建议设置MySQL的时候就把 default character set 设置成 utf8
      

  13.   

    建议用过滤器来处理!xml中的代码
    <filter>
       <filter-name>TestFilter</filter-name>
       <filter-class>org.net.TestFilter.TestFilter</filter-class>   <init-param>
       <param-name>config</param-name>
       <param-value>gb2312</param-value>
       </init-param>
      </filter>
      
      <filter-mapping>
       <filter-name>TestFilter</filter-name>
       <url-pattern>/*</url-pattern>
      </filter-mapping>
    过滤器中的代码:
    package org.net.TestFilter;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.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;public class TestFilter implements Filter { private FilterConfig config;
    private String econding="GBK";

    public void destroy() {
    } public void doFilter(ServletRequest arg0, ServletResponse arg1,
    FilterChain chain) throws IOException, ServletException {

    HttpServletRequest request=(HttpServletRequest)arg0;
    HttpServletResponse response=(HttpServletResponse)arg1;
    if(config!=null){
    econding=config.getInitParameter("config");
    }
    request.setCharacterEncoding(econding);
    response.setCharacterEncoding(econding);

    chain.doFilter(request, response);
    } public void init(FilterConfig config) throws ServletException {
    this.config=config;
    }}
      

  14.   

    设置过滤器 还有该成UTF-8
      

  15.   

    1z,首先要统一字符的编码,从数据库建库,到页面,要么都采用utf-8要么都采用gbk。
    2.解决程序中的乱码问题,可以编写过滤器,而spring已经给我们提供了,所以我们无需自己定义及
    <filter>
    <filter-name>SetCharacterEncoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
    </init-param>

    </filter>

    <filter-mapping>
    <filter-name>SetCharacterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    </filter-mapping>
      

  16.   

    建议用过滤器来处理!xml中的代码
    <filter>
      <filter-name>TestFilter</filter-name>
      <filter-class>org.net.TestFilter.TestFilter</filter-class>  <init-param>
      <param-name>config</param-name>
      <param-va……
    [/Quote] 还有该成UTF-8