如何解決Tomcat下中文亂碼問題?

現在將常見的亂碼問題分爲JSP頁面顯示中文亂碼、表單提交亂碼兩類。

1)JSP頁面中顯示中文亂碼

在JSP文件中使用page命令指定響應結果的MIME類型,如<%@ pagelanguage="java" contentType="text/html;charset=gb2312"%>

2)表單提交亂碼

表單提交時(post和Get方法),使用request.getParameter方法得到亂碼,這是因爲tomcat處理提交的參數時默認的是iso-8859-1,表單提交get和post處理亂碼問題不同,下面分別說明。
   (1)POST處理
對post提交的表單通過編寫一個過濾器的方法來解決,過濾器在用戶提交的數據被處理之前被調用,可以在這裏改變參數的編碼方式,過濾器的代碼如下:

Java代碼 複製代碼

  1. package example.util;      


  2. import java.io.IOException;      


  3. import javax.servlet.Filter;      

  4. import javax.servlet.FilterChain;      

  5. import javax.servlet.FilterConfig;      

  6. import javax.servlet.ServletException;      

  7. import javax.servlet.ServletRequest;      

  8. import javax.servlet.ServletResponse;      


  9. publicclass SetCharacterEncodingFilter implements Filter {      


  10. protected String encoding = null;  


  11. protected FilterConfig filterConfig = null;  


  12. protectedboolean ignore = true;  



  13. publicvoid destroy() {      


  14. this.encoding = null;  

  15. this.filterConfig = null;  


  16.     }      


  17. publicvoid doFilter(ServletRequest request, ServletResponse response,      

  18.      <STRONG><SPAN style="COLOR: #ff0000"> FilterChain chain) throws IOException, ServletException {      


  19. if (ignore || (request.getCharacterEncoding() == null)) {  

  20.       String encoding = selectEncoding(request);      

  21. if (encoding != null) {  

  22.        request.setCharacterEncoding(encoding);      

  23.       }      

  24.      }</SPAN>      

  25. </STRONG>          

  26. // Pass control on to the next filter    

  27.      chain.doFilter(request, response);      


  28.     }      

  29. publicvoid init(FilterConfig filterConfig) throws ServletException {      


  30. this.filterConfig = filterConfig;      

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

  32.      String value = filterConfig.getInitParameter("ignore");  

  33. if (value == null) {  

  34. this.ignore = true;  

  35.      } elseif (value.equalsIgnoreCase("true")) {  

  36. this.ignore = true;  

  37.      } elseif (value.equalsIgnoreCase("yes")) {  

  38. this.ignore = true;  

  39.      } else {  

  40. this.ignore = false;  

  41.      }      


  42.     }      


  43. protected String selectEncoding(ServletRequest request) {      


  44. return (this.encoding);      


  45.     }      


  46.    }  

package example.util;


   import java.io.IOException;


   import javax.servlet.Filter;

   import javax.servlet.FilterChain;

   importjavax.servlet.FilterConfig;

   importjavax.servlet.ServletException;

   importjavax.servlet.ServletRequest;

   import javax.servlet.ServletResponse;


   public classSetCharacterEncodingFilter implements Filter {


      protected Stringencoding = null;


      protectedFilterConfig filterConfig = null;


      protected booleanignore = true;



    public void destroy() {


     this.encoding = null;

     this.filterConfig =null;


    }


    public voiddoFilter(ServletRequest request, ServletResponse response,

FilterChain chain) throws IOException, ServletException {


         if(ignore || (request.getCharacterEncoding() == null)) {

      Stringencoding = selectEncoding(request);

      if(encoding != null) {

       request.setCharacterEncoding(encoding);

      }

     }


     // Pass control on tothe next filter

     chain.doFilter(request,response);


    }

   public void init(FilterConfigfilterConfig) 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 StringselectEncoding(ServletRequest request) {


     return(this.encoding);


    }


   }


文中紅色的代碼即爲處理亂碼的代碼。
     web.xml文件加入過濾器


Xml代碼 複製代碼

  1. <filter>

  2. <filter-name>Encoding</filter-name>

  3. <filter-class>

  4.            example.util.SetCharacterEncodingFilter      

  5. </filter-class>

  6. <init-param>

  7. <param-name>encoding</param-name>

  8. <param-value>gbk</param-value>

  9. <!--gbk或者gb2312或者utf-8-->

  10. </init-param>

  11. <init-param>

  12. <param-name>ignore</param-name>

  13. <param-value>true</param-value>

  14. </init-param>

  15. </filter>

<filter>

   <filter-name>Encoding</filter-name>

   <filter-class>

           example.util.SetCharacterEncodingFilter

    </filter-class>

   <init-param>

 <param-name>encoding</param-name>

  <param-value>gbk</param-value>

  <!--gbk或者gb2312或者utf-8-->

 </init-param>

 <init-param>

  <param-name>ignore</param-name>

  <param-value>true</param-value>

 </init-param>

</filter>

Xml代碼 複製代碼

  1. <filter-mapping>

  2. <filter-name>Encoding</filter-name>

  3. <servlet-name>/*</servlet-name>

  4. </filter-mapping>

<filter-mapping>

 <filter-name>Encoding</filter-name>

 <servlet-name>/*</servlet-name>

</filter-mapping>


(2) Get方法的處理
tomcat對post和get的處理方法不一樣,所以過濾器不能解決get的亂碼問題,它需要在其他地方設置。
打開<tomcat_home>\conf目錄下server.xml文件,找到對8080端口進行服務的Connector組件的設置部分,給這個組件添加一個屬性:URIEncoding="GBK"。修改後的Connector設置爲:

Java代碼 複製代碼

  1. <Connector port="8080" maxHttpHeaderSize="8192"

  2.               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"

  3.               enableLookups="false" redirectPort="8443" acceptCount="100"

  4.               connectionTimeout="20000" disableUploadTimeout="true" <SPAN style="COLOR: #ff0000">URIEncoding="GBK"</SPAN> />  

<Connector port="8080"maxHttpHeaderSize="8192"

             maxThreads="150" minSpareThreads="25"maxSpareThreads="75"

             enableLookups="false" redirectPort="8443"acceptCount="100"

             connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK" />


 *
注意修改後重新啓動tomcat才能起作用。

1.使用GBK編碼的解決方案
這個最簡單 遇到設置編碼的地方就是用GBK數據庫gbk 然後在使用個過濾器過濾編碼爲gbk一切搞定。
效果爲添加數據無亂碼 讀出無亂碼 數據庫管理工具無亂碼 到處sql結構和數據無亂碼

2.使用UTF-8編碼解決方案
所有編碼都設置爲UTF-8
數據庫編碼utf8
設置過濾器編碼utf8
數據庫連接?characterEncoding=utf8
然後在數據庫管理工具或mysql命令行 運行 SETcharacter_set_results = gbk;
效果爲添加數據無亂碼 讀出無亂碼 數據庫管理工具無亂碼 到處sql結構和數據時存在亂碼

3.頁面使用UTF8 數據庫使用latin1的解決方案
jap java tomcat 設置爲UTF-8
過濾器 utf8
數據庫連接?characterEncoding=latin1
數據庫其他latin1
然後在數據庫管理工具或mysql命令行 運行 SETcharacter_set_results = gbk;
效果爲添加數據無亂碼 讀出無亂碼 數據庫管理工具無亂碼 到處sql結構和數據時存在亂碼



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章