<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 14,  comments - 37,  trackbacks - 0
    先說我的問題吧, 然后再把各個高手的文章貼出來,
    我今天使用hibernate的時候改變了數據的編碼, 不再使用html encode ,
    結果就出現了編碼的問題了, 由于hibernate的本質是jdbc,就去改hibernate的設置, 現在發現原來是jdbc的格式問題,
    applicationContext.xml中的數據庫連接必須設置為<property>jdbc:mysql://localhost/dbname?useUnicode=true&characterEncoding=gb2312</property>不應該是 jdbc:mysql://localhost/dbname?useUnicode=true&characterEncoding=gb2312
    這是非常關鍵的!!!
    然后我的程序就正確的顯示中文了, 好了, 現在把高手的文章列表出來。
    http://www.ziki.cn/blog/archives/1155307798.html
    http://www.matrix.org.cn/thread.shtml?topicId=25997&forumId=27  這篇超贊!
    http://www.zhuoda.org/lunzi/60912.html

    下面把那個非常棒的文章轉來珍惜一下
    本文以最常見的JSP+MySQL+Tomcat+Apache亂碼解決為例,望能為你的環境配置起到拋磚引玉之效!

      亂碼問題已歷來已久,在開源環境下,亂碼問題更是令程序員措手不及。本人在Unix(Freebsd)下的一次亂碼經歷可謂經典,故撰以此文以滋效尤!

      我將本次所遇亂碼歸為三類:

     ?。保撁孀址麃y碼

     ?。玻涗涳@示亂碼

     ?。常畆equest傳遞亂碼

      以下將對上述三類亂碼進行解析:

    一.頁面字符亂碼:

      1.大小寫不一致:

    org.apache.jasper.JasperException: /top.jsp(1,1) Page directive: illegal to have multiple occurrences of contentType with different values (old: text/html;charset=gb2312, new: text/html;charset=GB2312)

     ?。玻g隔不一致:

    org.apache.jasper.JasperException: /top.jsp(1,1) Page directive: illegal to have multiple occurrences of contentType with different values (old: text/html; charset=GB2312, new: text/html;charset=GB2312)

    *解決方案:

    首先,在Apache中增加AddDefaultCharset GB2312或AddDefaultCharset GBK

    其次,統一使用頁面編碼定義,如:<%@page contentType="text/html;charset=GB2312"%>

    *注:GB2312為GBK之子集。

    二.記錄顯示亂碼:

     ?。保甅ySQL默人語言為latin1_swedish_ci,即拉丁語,所以取出的中文全是亂碼。

    *解決方案:

     ?。保畬harset設為8859_1即:<%@page contentType="text/html;charset=8859_1"%>

      這個方法只能暫時緩解字符顯示問題,并權益之計。因為8859_1為字節型字庫,并非字型字庫,故在非全角狀態下,將出現半字亂碼,表現為“?”。

     ?。玻跀祿爝B接語句中加上?useUnicode=true;characterEncoding=GBK,如:
    jdbc:mysql://localhost/dbname?useUnicode=true;characterEncoding=GBK

    * 注:一般教科書上都會加上localhost:3306,因為默認端口為3306,故可舍去!同時,請使用連接池的朋友注意,在注冊xml文件時,是不可 以單獨出現“;”的,所以必須使用“&”,即:jdbc:mysql://localhost/dbname?useUnicode= true&characterEncoding=GBK。

      否則提示出錯:

    Parse Fatal Error at line 213 column 91: The reference to entity "characterEncoding" must end with the ';' delimiter.
    org.xml.sax.SAXParseException: The reference to entity "characterEncoding" must
    end with the ';' delimiter.
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Un
    known Source)

      也曾有人提意:在MySQL的my.ini文件中加入default-character-set=gbk,本人不贊同此法,因為這樣破壞了原有的環境,除非這是MySQL的第一個站點。

    三.request傳遞亂碼:

      ?。保苍S,此時你已經可以正常使用系統了,那么恭喜~亂碼問題已經離開你了!但是,大伙通常都沒那么走運,亂碼問題依舊存在。也許,這時你向數據庫添加 了一條記錄以測試系統,可是此時顯示出的還是亂碼。那么可以肯定是Request參數傳遞出錯!那么先寫個測試語句:<%= request.getParameter(“Para”) %>,OK,果然是亂。那么,現在有兩種解決方法。

    *解決方案:

     ?。保由线@條語句:request.setCharacterEncoding("gbk");
    在一/兩頁上可行,但此法也非權益之計。

      2.注冊SetCharacterEncodingFilter類:

      首先,編寫SetCharacterEncodingFilter.java文件,代碼如下:
     1package cn.com.jsp;
     2
     3import java.io.IOException;
     4import javax.servlet.Filter;
     5import javax.servlet.FilterChain;
     6import javax.servlet.FilterConfig;
     7import javax.servlet.ServletException;
     8import javax.servlet.ServletRequest;
     9import javax.servlet.ServletResponse;
    10import javax.servlet.UnavailableException;
    11
    12public class SetCharacterEncodingFilter implements Filter {
    13    protected String encoding = null;
    14    protected FilterConfig filterConfig = null;
    15    protected boolean ignore = true;
    16
    17    public void destroy() {
    18        this.encoding = null;
    19        this.filterConfig = null;
    20    }

    21
    22    public void doFilter(ServletRequest request, ServletResponse response,
    23                         FilterChain chain) throws IOException,
    24            ServletException {
    25
    26        // Conditionally select and set the character encoding to be used
    27        if (ignore || (request.getCharacterEncoding() == null)) {
    28            String encoding = selectEncoding(request);
    29            if (encoding != null{
    30                request.setCharacterEncoding(encoding);
    31            }

    32        }

    33
    34        // Pass control on to the next filter
    35        chain.doFilter(request, response);
    36
    37    }

    38
    39    public void init(FilterConfig filterConfig) throws ServletException {
    40
    41        this.filterConfig = filterConfig;
    42        this.encoding = filterConfig.getInitParameter("encoding");
    43        String value = filterConfig.getInitParameter("ignore");
    44        if (value == null{
    45            this.ignore = true;
    46        }
     else if (value.equalsIgnoreCase("true")) {
    47            this.ignore = true;
    48        }
     else if (value.equalsIgnoreCase("yes")) {
    49            this.ignore = true;
    50        }
     else {
    51            this.ignore = false;
    52        }

    53
    54    }

    55
    56    protected String selectEncoding(ServletRequest request) {
    57        return (this.encoding);
    58    }

    59
    60}

    61


     此文件為request過濾類,在全局編譯前需進行注冊。

      注冊文件為:<%wwwroot%>/WEB-INF/web.xml。

      在此文件中加入如下代碼即可:
     1<web-app>
     2  <display-name>wwwroot</display-name>
     3  <description>MySQL Test App</description>
     4  <filter>
     5    <filter-name>setCharacterEncodingFilter</filter-name>
     6    <display-name>setCharacterEncodingFilter</display-name>
     7    <description>setCharacterEncodingFilter</description>
     8    <filter-class>cn.com.jsp.SetCharacterEncodingFilter</filter-class>
     9    <init-param>
    10      <param-name>encoding</param-name>
    11      <param-value>GBK</param-value>
    12    </init-param>
    13  </filter>
    14  <filter-mapping>
    15    <filter-name>setCharacterEncodingFilter</filter-name>
    16    <url-pattern>/*</url-pattern>
    17  </filter-mapping>
    18……
    19</web-app>
    20

     OK,現在可以編譯你的SetCharacterEncodingFilter.java文件啦!

      至此,亂碼將與你格格不入!
    posted on 2007-07-10 10:24 冰封的愛 閱讀(346) 評論(0)  編輯  收藏 所屬分類: J2EE
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿(3)

    隨筆檔案

    文章分類

    文章檔案

    相冊

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 最新亚洲成av人免费看| 在线看片人成视频免费无遮挡| www国产亚洲精品久久久| 亚洲国产成人AV在线播放| 在线中文高清资源免费观看| 亚洲 欧洲 自拍 另类 校园| 免费电视剧在线观看| 国产亚洲一卡2卡3卡4卡新区| 午夜亚洲国产理论片二级港台二级| 在线人成精品免费视频| 亚洲精品成人av在线| 日本片免费观看一区二区| 亚洲春色另类小说| 性短视频在线观看免费不卡流畅| 亚洲一区二区三区免费在线观看| 日韩一区二区a片免费观看| 亚洲精品无码久久久久A片苍井空 亚洲精品无码久久久久YW | 久久亚洲成a人片| 一个人免费日韩不卡视频| 2022年亚洲午夜一区二区福利| 国产在线jyzzjyzz免费麻豆| 亚洲香蕉久久一区二区| 国产成人青青热久免费精品| gogo免费在线观看| 国产AV无码专区亚洲AVJULIA| 99精品视频在线免费观看 | 亚洲欧美成人综合久久久| 亚洲欧洲日产国码高潮αv| 国产免费网站看v片在线| 亚洲大尺码专区影院| 日韩免费高清视频网站| 手机看片国产免费永久| 亚洲国产精品美女| 亚洲高清无码综合性爱视频| 久久免费精品视频| 亚洲精品无码专区在线| 亚洲熟妇av一区二区三区漫画| 2021在线永久免费视频| 水蜜桃视频在线观看免费| 亚洲国产成人久久精品影视| 毛片基地免费观看|