在tomcat環境下的中文亂碼問題

來源:互聯網
上載者:User

   
以前都是用公司現成的架構開發一些項目,但最近自己利用Struts+Spring+hibernate開發項目的時候才發現自己會的技術太少了,真是問題多多啊,革命尚未成功,同志還需努力。。。。

   
剛開始的時候,將JSP頁面利用<%@page contentType="text/html;
charset=GBK"%>轉換頁面上漢字的編碼方式,頁面上的漢字可以正常顯示了,但JSP頁面上的中文資料傳入BEAN的時候還是出現亂碼問題,從action裡面get出來的漢字都是亂碼,在網上尋找了一些資料,發現可以在formBean裡面添加toGBK方法:

  1. private String toGBK(String str)throws java.io.UnsupportedEncodingException
  2.  {
  3.     return new String(str.getBytes("ISO-8859-1"),"GBK");
  4.  }

然後在每個set方法中將傳進來的值進行轉換,如:

  1. public void setXgrq(Date xgrq)
  2.    {
  3.        this.xgrq = this.toGBK(xgrq).trim;
  4.    }

但這樣雖然解決了通過form傳遞中文的問題,但不能解決其他方式傳值問題(不通過form),如:通過location.href傳值的方式,於是想到寫一個filter來過濾所有的傳值調用,由於以前沒有寫過filter,所以現在也把filter的代碼帖出來,便於記憶:

  1. package com.wmf.struts;
  2.  
  3. import java.io.IOException;
  4. import javax.servlet.*;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletResponse;
  7. public class servfilter extends HttpServlet implements Filter {
  8.  protected String encoding = "GBK";
  9.  protected FilterConfig filterConfig;
  10.  
  11.  public void destroy() {
  12.   // TODO 自動產生方法存根
  13.   this.encoding = null;
  14.   this.filterConfig = null;
  15.  }
  16.  public void doFilter(ServletRequest request, ServletResponse response,
  17.    FilterChain filterChain)
  18.                         throws IOException, ServletException {
  19.   // TODO 自動產生方法存根
  20.   request.setCharacterEncoding(this.encoding);
  21.   response.setCharacterEncoding(this.encoding);
  22.   ((HttpServletResponse)response).setHeader("Pragma","No-cache");
  23.   ((HttpServletResponse)response).setHeader("Cache-Control","no-cache");
  24.   ((HttpServletResponse)response).setHeader("Expires","0");
  25.   filterChain.doFilter(request, response);
  26.  }
  27.  public void init(FilterConfig filterConfig) throws ServletException {
  28.   // TODO 自動產生方法存根
  29.   this.filterConfig = filterConfig;
  30.   this.encoding = this.filterConfig.getInitParameter("encoding");
  31.  }
  32. }


在web.xml中也需要增加如下配置:

  1. <filter>
  2.     <filter-name>servfilter</filter-name>
  3.     <filter-class>com.wmf.struts.servfilter</filter-class>
  4.     <init-param>
  5.         <param-name>encoding</param-name>
  6.         <param-value>GBK</param-value>
  7.     </init-param>
  8.   </filter>
  9.   <filter-mapping>
  10.     <filter-name>servfilter</filter-name>
  11.     <url-pattern>/*</url-pattern>
  12.   </filter-mapping>
  13. </filter>

   
結果測試了一把,單步調試的時候程式已經跳入filter了,但location.href傳遞的中文值亂碼依舊,無奈啊。。。

   
實在沒辦法了,就上CSDN找高手求救,哈哈,下面是他們的回複:

   
要是tomcat,則編碼問題建議統一用utf-8,我們就一直使用的很好
   
需要在server.xml裡面的connector裡面務必設定如下參數:
   
URIEncoding="UTF-8" useBodyEncodingForURI="true"

   
**************************************************************
    傳值出現中文亂碼你使用的是tomcat的吧
   
是url傳值中文亂碼吧
    先把
server.xml 中的<Connector 中加入 URIEncoding="GBK"
   
url傳中文最好先轉換下編碼
   
URLEncoder.encode("漢字","GBK")
   
接受後在解碼
   
URLDecoder.decode("漢字","GBK");

   
果然,在Connector中加入URIEncoding="UTF-8"
useBodyEncodingForURI="true"之後,問題統統搞定了,無論是form還是location.href傳的中文參數都正確了,o(∩_∩)o...哈哈

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.