utf8亂碼解決方案[適合tomcat部署的jsp應用]

來源:互聯網
上載者:User

 

1.       java類: CharacterEncodingFilter

 import javax.servlet.*;

import java.io.IOException;

 

 public class CharacterEncodingFilter implements Filter {

 

    private String encoding = "UTF-8";

    private boolean forceEncoding = true;

 

    /**

     * Set the encoding to use for requests. This encoding will be

     * passed into a ServletRequest.setCharacterEncoding call.

     * <p>Whether this encoding will override existing request

     * encodings depends on the "forceEncoding" flag.

     */

    public void setEncoding(String encoding) {

        this.encoding = encoding;

    }

 

    /**

     * Set whether the encoding of this filter should override existing

     * request encodings. Default is "false", i.e. do not modify encoding

     * if ServletRequest.getCharacterEncoding returns a non-null value.

     */

    public void setForceEncoding(boolean forceEncoding) {

        this.forceEncoding = forceEncoding;

    }

 

    public void init(FilterConfig filterConfig) throws ServletException {

        this.encoding = filterConfig.getInitParameter("encoding");

        String force = filterConfig.getInitParameter("forceEncoding");

        this.forceEncoding = (force == null) || Boolean.valueOf(force).booleanValue() ;

    }

 

    public void doFilter(ServletRequest servletRequest,

                         ServletResponse servletResponse,

                         FilterChain filterChain) throws IOException, ServletException {

        if (this.forceEncoding || servletRequest.getCharacterEncoding() == null) {

            servletRequest.setCharacterEncoding(this.encoding);

        }

        filterChain.doFilter(servletRequest, servletResponse);

    }

 

    public void destroy() {

 

    }

 

}

 

2.       在web.xml設定檔裡加Filters Configurations

 <!-- Filters Configurations        -->

       <filter>

              <filter-name>CHARACTER_ENCODING</filter-name>

              <filter-class>

                     common.encoding.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>CHARACTER_ENCODING</filter-name>

              <url-pattern>/*</url-pattern>

       </filter-mapping>

 

3.資料庫建立為UTF-8格式
4.項目右鍵屬性為UTF-8格式
5.所有頁面申明為UTF-8
6.JDBC URL設為:UTF-8

jdbc:mysql://localhost:3306/databaseuse?useUnicode=true&characterEncoding=utf-8
7.資料庫Driver選擇UTF-8格式
8.Tomcat編碼改成UTF-8

在server.xml裡面增加URIEncoding="UTF-8"

<Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8" />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.