tomcat亂碼問題

首先Java(包括JSP)源文件中很可能包含有中文,而Java和JSP源文件的保存方式是基於字節流的,如果Java和JSP編譯成class文件過程中,使用的編碼方式與源文件的編碼不一致,就會出現亂碼。基於這種亂碼,建議在Java文件中儘量不要寫中文(註釋部分不參與編譯,寫中文沒關係),如果必須寫的話,儘量手動帶參數-ecoding GBK或-ecoding gb2312編譯;對於JSP,在文件頭加上<%@ page contentType="text/html;charset=GBK"%>或<%@ page contentType="text/html;charset=gb2312"%>基本上就能解決這類亂碼問題。

  本文要重點討論的是第二類亂碼,即Java程序與其他存儲媒介交互時產生的亂碼。很多存儲媒介,如數據庫,文件,流等的存儲方式都是基於字節流的,Java程序與這些媒介交互時就會發生字符(char)與字節(byte)之間的轉換,例如從頁面提交表單中提交的數據在Java程序裏顯示亂碼等情況。

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

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

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

     2)表單提交亂碼   

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

<span style="background-color:rgb(255,255,255)">package example.util;
    
    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;
    
       protected boolean ignore = true;
    
  
     public void destroy() {
    
      this.encoding = null;
      this.filterConfig = null;
    
     }
    
     public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain) throws IOException, ServletException {
    
          if (ignore || (request.getCharacterEncoding() == null)) {
       String encoding = selectEncoding(request);
       if (encoding != null) {
        request.setCharacterEncoding(encoding);
       }
      }
      // Pass control on to the next filter
      chain.doFilter(request, response);
    
     }
    public void init(FilterConfig filterConfig) 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 String selectEncoding(ServletRequest request) {
    
      return (this.encoding);
    
     }
    
    }
</span>

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

<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代碼


<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設置爲:

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


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



原文:https://blog.csdn.net/zzq900503/article/details/11642709 
 

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