如何在开发和编译代码时指定字符集为UTF-8?如何设定数据库连接方式是UTF-8?

解决方案 »

  1.   

    import java.io.IOException;
    import javax.servlet.Filter;
    import javax.servlet.FilterConfig;
    import javax.servlet.FilterChain;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;/**
     * ignore If set to "true", any character encoding
     * specified by the client is ignored, and the value returned by the
     * <code>selectEncoding()</code> method is set.  If set to "false,
     * <code>selectEncoding()</code> is called <strong>only</strong> if the
     * client has not already specified an encoding.  By default, this
     * parameter is set to "true".
     */
    public class SetCharacterEncodingFilter
        implements Filter {
      protected String encoding;
      protected FilterConfig fConfig;
      protected boolean ignore;
      {
        this.encoding = "";
        this.fConfig = null;
        this.ignore = true;
      }  public SetCharacterEncodingFilter() {
      }  /**
       * Place this filter into service
       *
       * @param fConfig The filter configuration object
       * @throws ServletException
       */
      public void init(FilterConfig fConfig) throws ServletException {
        this.fConfig = fConfig;
        this.encoding = fConfig.getInitParameter("encoding");
        String ignoreValue = fConfig.getInitParameter("ignore");
        if (ignoreValue == null) {
          this.ignore = true;
        }
        else if (ignoreValue.equalsIgnoreCase("true")) {
          this.ignore = true;
        }
        else if (ignoreValue.equalsIgnoreCase("yes")) {
          this.ignore = true;
        }
        else {
          this.ignore = false;
        }
      }  /**
       * 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 response The servlet response we are creating
       * @param chain The filter chain we are processing
       * @throws IOException if an input/output error occurs
       * @throws ServletException if a servlet error occurs
       */
      public void doFilter(ServletRequest request, ServletResponse response,
                           FilterChain chain) throws ServletException, IOException {
        if (this.ignore || (request.getCharacterEncoding() == null)) {
          String tempEncoding = this.selectEncoding(request);
          if (tempEncoding != null) {
            request.setCharacterEncoding(tempEncoding);
          }
        }
        System.out.println(request.getCharacterEncoding());
        chain.doFilter(request, response);
        fConfig.getServletContext().log( (new ServletException()).getMessage());
        fConfig.getServletContext().log( (new IOException()).getMessage());
        return;
      }  public void destroy() {
        encoding = "";
        fConfig = null;
      }  protected String selectEncoding(ServletRequest request) {
        return this.encoding;
      }}
      

  2.   

    <filter>
        <filter-name>EncodingFilter</filter-name>
        <description>Character Encoding Filter</description>
        <filter-class>SetCharacterEncodingFilter</filter-class>
        <init-param>
          <param-name>ignore</param-name>
          <param-value>true</param-value>
        </init-param>
        <init-param>
          <param-name>encoding</param-name>
          <param-value>utf-8</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <servlet-name>action</servlet-name>
      </filter-mapping>
      

  3.   

    楼上说的,我知道,它不是我想要的答案!
    它能处理的只是经过servlet的request,但是还有很多是不经过servlet的!
    比如直接从数据库中读取数据?
      

  4.   

    先用show variables like '%char%';查看编码
    mysql> show variables like '%char%';1,修改编码
    找到mysql下的my.ini文件中
    [mysqld]
    default-character-set=utf8
    [client]
    default-character-set=utf8
    2,再查看,发现database的编码可能还是默认的latin1
    3,修改数据库编码
    ALTER DATABASE dbname CHARACTER SET utf8;
    4,再查看,可能只剩一个system的编码不一致,但这没关系
    5,这样把mysql数据库编码设置好了,注意这时一定要重新建表,因为表的默认编码还是以前的,可以导出原来表的sql脚本可看到,我原来因为这点痛苦了很久
    6,servlet方面的编码就是用网上的fiter方法解决