風人園

          弱水三千,只取一瓢,便能解渴;佛法無邊,奉行一法,便能得益。
          隨筆 - 99, 文章 - 181, 評論 - 56, 引用 - 0
          數據加載中……

          解決java中的中文亂碼問題(ZT)

          一般在傳送時使用的encoding:
          使用GET 的方式:
          String test = new String((request.getParameter("test")).getBytes("ISO-8859-1"),"GBK");

          使用POST 的方式:
          request.setCharacterEncoding("GBK");

          pageEncoding及contentType的作用:
          <%@ page pageEncoding="GBK" %>
          pageEncoding是當jsp轉譯成_jsp.java時使用的encoding.預設是iso8859_1.
          然後_jsp.java編譯成_jsp.class是使用utf-8作為encoding.
          response.setContentType("text/html; charset=GBK");
          or
          <%@ page contentType="text/html;charset=GBK" %>
          就是輸出到瀏覽器時的編碼,預設是iso8859_1.
          這樣瀏覽器才知道應該用甚麼charset來顯示.

          資料庫輸出到jsp出現亂碼:
          第一,檢查一下你database里的資料是否亂碼.
          如果database里的資料是亂碼,檢查一下再進入資料庫時request接收後資料是否是亂碼.
          即忘了加入request.setCharacterEncoding(String charset);
          request.setCharacterEncoding(String charset)把表單傳送過來的資料以charset的字型來encoding.
          如果接收時是正常的.但資料庫是亂碼,
          那就檢查一下database的編碼設定.

          如果資料庫的資料正常.但輸出到jsp的資料是亂碼.
          以mysql為例:
          String connect = "jdbc:mysql://localhost/dbname?user=&password=&useUnicode=true&characterEncoding=GBK";
          就上面的mysql的例子.
          需要設定useUnicode為true.
          而characterEncoding=GBK必須與contentType的charset一樣.
          mysql的設定檔my.ini:
          [client] default-character-set=GBK
          [mysqld] default-character-set=GBK


          以下連結則介紹了access的jdbc:odbc的編碼:
          http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/bridge.html

          include資料出現亂碼:
          舊的版本tomcat是不能在每一頁jsp中加入
          <%@ page contentType="text/html; charset=GBK" %>
          tomcat 5.x之後是可以每一頁都加入上面那一句的.
          會必須include的page及被include的page的編碼要相同.連大小寫都要相同.

          但這樣做不是最好的方法.

          其實可以在你的web application底下的web.xml的<web-app>里加入:


          <?xml version="1.0" encoding="ISO-8859-1"?>
          <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
          ?? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          ?? xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
          ?? http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"version="2.4">
          .........
          .........??
          <jsp-config>
          <jsp-property-group>??
          ??<description>jsp encoding example</description>
          ??<display-name>JSPConfiguration</display-name>
          ??<url-pattern>*.jsp</url-pattern>
          ??<el-ignored>true</el-ignored>
          ??<page-encoding>GBK</page-encoding>
          ??<scripting-invalid>false</scripting-invalid>
          ??<include-prelude></include-prelude>
          ??<include-coda></include-coda>
          ??<description>html encoding example</description>
          ??<display-name>JSPConfiguration</display-name>??
          ??<url-pattern>*.html</url-pattern>??
          ??<el-ignored>true</el-ignored>
          ??<page-encoding>GBK</page-encoding>
          ??<scripting-invalid>false</scripting-invalid>
          ??<include-prelude></include-prelude>??
          ??<include-coda></include-coda>
          </jsp-property-group>
          </jsp-config>
          .........
          .........
          </web-app>



          apache整合tomcat 中文問題:
          apache+tomcat+JK2集成時,http的請求是通過jk2的ajp13轉到tomcat的8009端口處理的,
          所以要修改tomcat/conf/server.xml中的以下兩項
          1 <Connector port="8080" …… URIEncoding="GBK" >
          2 <Connector port="8009" …… URIEncoding="GBK" >
          都加上URIEncoding="GBK"。

          posted on 2006-06-20 16:32 風人園 閱讀(246) 評論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 天镇县| 德保县| 威远县| 信宜市| 浮梁县| 高雄县| 福鼎市| 南通市| 嘉义县| 武平县| 苍南县| 闽侯县| 吴川市| 南昌市| 合肥市| 武汉市| 同江市| 天津市| 长岭县| 和林格尔县| 沙田区| 得荣县| 堆龙德庆县| 定安县| 自治县| 眉山市| 公安县| 镇平县| 论坛| 巴东县| 正蓝旗| 大竹县| 成安县| 贺州市| 陇西县| 达孜县| 循化| 宁德市| 高密市| 全椒县| 陇川县|