posts - 32, comments - 153, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          又談亂碼問題解決

          Posted on 2007-01-23 21:12 Zou Ang 閱讀(4384) 評論(6)  編輯  收藏 所屬分類:

          有人說,亂碼問題一直跟中國的程序員特別有緣,真是再同意不過了,不管是Struts,JSF,JSP,還是MySQL,Tomcat,全都或多或少有亂碼的問題。
          一般的做法有用Filter:

          < filter >
          ????
          < filter-name > Set?Character?Encoding </ filter-name >
          ????
          < filter-class > org.springframework.web.filter.CharacterEncodingFilter </ filter-class >
          ????
          < init-param >
          ??????
          < param-name > encoding </ param-name >
          ??????
          < param-value > GBK </ param-value >
          ????
          </ init-param >
          ????
          < init-param >
          ??????
          < param-name > ignore </ param-name >
          ??????
          < param-value > true </ param-value >
          ????
          </ init-param >
          ??
          </ filter >
          ??
          < filter-mapping >
          ????
          < filter-name > Set?Character?Encoding </ filter-name >
          ????
          < url-pattern > *.do </ url-pattern >
          ??
          </ filter-mapping >
          ??
          < filter-mapping >
          ????
          < filter-name > Set?Character?Encoding </ filter-name >
          ????
          < url-pattern > *.jsp </ url-pattern >
          ??
          </ filter-mapping >
          ??
          < filter-mapping >
          ????
          < filter-name > Set?Character?Encoding </ filter-name >
          ????
          < url-pattern > *.html </ url-pattern >
          ??
          </ filter-mapping >
          ??
          < filter-mapping >
          ????
          < filter-name > Set?Character?Encoding </ filter-name >
          ????
          < url-pattern > *.htm </ url-pattern >
          ??
          </ filter-mapping >


          的,有用

          <% request.setCharacterEncoding( " GBK " ); %>

          的,還有用

          <% @?page?contentType = " text/html;?charset=GBK " ?pageEncoding = " GBK " %>


          的,還可以用

          <% ?String?name? = ? new ?String(request.getParameter( " name " ).getBytes( " 8859_1 " ),? " GB2312 " );? %>


          昨天就在做項目的過程中,發現用URL傳request參數的時候,在第二個頁面上得到亂碼的問題。把上面幾種方法都試了一下還是不行。仔細追蹤了一下,發現在頁面的源代碼上中文是正常的,一直到URL還是中文正常,可是在后臺的Action里面log出來就成了亂碼了,于是猜想是在request封裝的過程中把中文變成亂碼了,以致于后臺直接就是取到的亂碼。在后臺Action中Set入中文,頁面上正常顯示,說明Struts的中文已經不存在問題。剩下的,應該就只有doGet和doPost方法的問題了。找了一下tomcat的配置文件,發現只要在server.xml中:

          ???? <!-- ?Define?a?non-SSL?HTTP/1.1?Connector?on?port?8080? -->
          ????
          < Connector? port ="8080" ?maxHttpHeaderSize ="8192"
          ???????????????maxThreads
          ="150" ?minSpareThreads ="25" ?maxSpareThreads ="75"
          ???????????????enableLookups
          ="false" ?redirectPort ="8443" ?acceptCount ="100"
          ???????????????connectionTimeout
          ="20000" ?disableUploadTimeout ="true" />
          ????
          <!-- ?Note?:?To?disable?connection?timeouts,?set?connectionTimeout?value
          ?????to?0?
          -->

          改為

          ???? <!-- ?Define?a?non-SSL?HTTP/1.1?Connector?on?port?8080? -->
          ????
          < Connector? port ="8080" ?maxHttpHeaderSize ="8192"
          ???????????????maxThreads
          ="150" ?minSpareThreads ="25" ?maxSpareThreads ="75"
          ???????????????enableLookups
          ="false" ?redirectPort ="8443" ?acceptCount ="100"
          ???????????????connectionTimeout
          ="20000" ?disableUploadTimeout ="true" ?URIEncoding ="GBK" />
          ????
          <!-- ?Note?:?To?disable?connection?timeouts,?set?connectionTimeout?value
          ?????to?0?
          -->

          就是加上URIEncoding="GBK"就萬事大吉了。
          至此,應該再困難的亂碼問題都解決了吧。就是要在頁面上、數據庫中、request里、doGet、doPost方法里面都是中文!看你還有什么地方躲??


          評論

          # re: 又談亂碼問題解決  回復  更多評論   

          2007-01-23 23:18 by 小車馬
          不錯,這個亂碼問題,可能和tomcat容器有關,呵呵

          樓主,潛力貼論壇(http://content.uu1001.com)是我個人的一個設想,如果你對java非常的專注,并且愿意交我這個朋友,可以發郵件給我(lbw070105@gmail.com),希望我們可以一起發展它。

          # re: 又談亂碼問題解決  回復  更多評論   

          2007-01-24 10:06 by swiftybin
          好文章!!收藏了。多謝有心的樓主!

          # re: 又談亂碼問題解決  回復  更多評論   

          2007-01-24 10:09 by 小石頭
          好!!

          # re: 又談亂碼問題解決  回復  更多評論   

          2007-01-24 13:53 by BeanSoft
          Tomcat 5 默認情況下, 用 request.setCharacterEncoding( " GBK " ); 這樣的方法可以獲得 POST 方法提交的中文, 而 GET 方法無論如何都需要強制轉換編碼
          String name = new String(request.getParameter( " name " ).getBytes( " 8859_1 " ), " GB2312 " );
          一般來說做個過濾器來對GET和POST做不同的判斷處理即可.
          至于樓主修改 Tomcat 配置的方式, 自己開發的應用還可以, 要是虛擬主機的話, 就不管用了.

          # re: 又談亂碼問題解決  回復  更多評論   

          2007-01-25 15:50 by 冷面閻羅
          那weblogic的情況應該和tomcat差不多吧?還是
          看了weblogic的config.xml好像沒有地方改

          # re: 又談亂碼問題解決[未登錄]  回復  更多評論   

          2007-01-25 16:00 by BeanSoft
          Weblogic 的有自己的參數, 所以不是很推薦用這種改服務器參數的辦法解決中文問題... 不同的服務器參數都不一樣, 就是 Weblogic 自己的版本不一樣, 參數也不一樣...

          參考: http://dev2dev.bea.com.cn/techdoc/200311546.html
          主站蜘蛛池模板: 石城县| 余干县| 海原县| 丰都县| 舞钢市| 都江堰市| 克东县| 上犹县| 原平市| 清流县| 凤城市| 石家庄市| 雷波县| 婺源县| 巴青县| 漳州市| 会宁县| 青岛市| 祁连县| 诏安县| 亚东县| 朔州市| 万盛区| 安多县| 长治市| 阜阳市| 永登县| 阳高县| 苏州市| 霞浦县| 遂溪县| 甘泉县| 礼泉县| 石泉县| 芒康县| 长沙县| 横峰县| 紫云| 义马市| 武夷山市| 镇赉县|