學海拾遺

          生活、技術、思想無處不在學習
          posts - 52, comments - 23, trackbacks - 0, articles - 3
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Java Web的中文問題

          Posted on 2008-03-20 09:43 tanzek 閱讀(505) 評論(0)  編輯  收藏

          摘自王俊標編著的《精通Java Web開發-基于Struts EJB Hibernate JBuilder》一書:

          1、頁面請求
          ???采用Filter過濾器方式:
          ??????????????????web.xml片段

          ?1 ……
          ?2 <!-- ?filter?配置? -->
          ?3 < filter >
          ?4 ???? <!-- ?filter?別名? -->
          ?5 ???????? < filter-name > encodingfilter </ filter-name >
          ?6 ???????? <!-- ?類文件位置? -->
          ?7 ???????? < filter-class > login3.EncodingFilter </ filter-class >
          ?8 ????????
          ?9 ???????? <!-- ?定義屬性encoding="gb2312"?即指定編碼類型? -->
          10 ???????? < init-param >
          11 ???????????? < desription > encode?to?gb2312 </ desription >
          12 ???????????? < param-name > encoding </ param-name >
          13 ???????????? < param-value > gb2312 </ param-value >
          14 ???????? </ init-param >
          15 </ filter >
          16
          17 <!-- ?filter?映射配置? -->
          18 < filter-mapping >
          19 ???? <!-- ?定義該filter?處理所有格式的請求? -->
          20 ???? < filter-name > encodingfilter </ filter-name >
          21 ???? < url-pattern > /* </ url-pattern >
          22 </ filter-mapping >
          23 ……

          ?????????EncodingFilter.java

          ?1 import ?java.io.IOException;
          ?2
          ?3 import ?javax.servlet. * ;
          ?4 import ?javax.servlet.http.HttpServlet;
          ?5
          ?6 public ? class ?EncodingFilter? extends ?HttpServlet?
          ?7 ???? implements ?Filter? {
          ?8 ????
          ?9 ???? private ?FilterConfig?filterConfig;
          10 ????
          11 ???? private ?String?targetEncoding? = ? null ;
          12 ????
          13 ???? public ? void ?init(FilterConfig?filterConfig)? throws ?ServletException? {
          14 ???????? this .filterConfig? = ?filterConfig;
          15 ???????? this .targetEncoding? = ? this .filterConfig.getInitParameter( " encoding " );
          16 ????}

          17
          18 ???? public ? void ?doFilter(ServletRequest?request,?ServletResponse?response,
          19 ????????????FilterChain?chain)? throws ?IOException,?ServletException? {
          20 ???????? try ? {
          21 ????????????System.out.println( " targetEncoding?: " ? + ?targetEncoding? + ? " \n " );
          22 ????????????request.setCharacterEncoding(targetEncoding);
          23 ????????????chain.doFilter(request,?response);
          24 ????????}
          ? catch ?(ServletException?sx)? {
          25 ????????????filterConfig.getServletContext().log(sx.getMessage());
          26 ????????}
          ? catch ?(IOException?iox)? {
          27 ????????????filterConfig.getServletContext().log(iox.getMessage());
          28 ????????}

          29 ????}

          30 ????
          31 ???? public ? void ?destroy()? {
          32 ???????? this .filterConfig? = ? null ;
          33 ???????? this .targetEncoding? = ? null ;
          34 ????}
          ????
          35 }

          36


          2、Web容器編碼
          ??????在Tomcat的的源代碼中的getParameter()方法,用戶調用request.getParameter()方法時,首先調用parsetParameters()方法,在這里面將編碼進行轉換。如:
          ??????String s_target = new String (s_source.getBytes("iso-8859-1"), "gb2312);

          3、數據庫
          ??????MySQL中文問題的主要原因是:它對數據進行編碼時,采用了和底層的操作系統所不同的編碼。通常MySQL在中文Windows平臺上正確的編碼方式是GBK。因此只要將MySQL安裝目錄下的my.ini文件中的default-character-set選項設為GBK即可。

          4、響應結果
          ??????對于Servlet的代碼處理,可以使用:
          ?????????……
          ?????????respsonse.setContentType("text/html;charset=GB2312");
          ?????????……
          ??????對于JSP文件,可以在開頭部分添加如下聲明:
          ?????????……
          ?????????<%@ page contentType="text/html;charset=GB2312" %>
          ?????????……
          ??????對于HTML頁面文件,則在開頭部分添加聲明如下:
          ?????????……
          ?????????<head>
          ????????????<META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=GB2312">
          ?????????</head>
          ?????????……
          5、XML文件
          ??????在采用"UTF-8"方式編碼的情況下,使用XML文件存儲中文數據常常會出現亂碼的問題,建議使用"GB2312":
          ??????<?xml version='1.0' encoding="GB2312"


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 赞皇县| 合作市| 青神县| 鸡东县| 卢龙县| 呼图壁县| 宁远县| 敦煌市| 沙洋县| 阳江市| 马公市| 石楼县| 卓资县| 西平县| 潮安县| 浏阳市| 乐陵市| 平安县| 平凉市| 台江县| 陆丰市| 炉霍县| 洪泽县| 翁牛特旗| 大化| 台江县| 修水县| 景洪市| 沛县| 乌兰县| 绥棱县| 古蔺县| 平顺县| 黎川县| 来凤县| 仁寿县| 清水县| 金塔县| 大关县| 昌都县| 黄龙县|