小閣飛空 一池碧映垂楊路 絳云深處 聽盡瀟瀟雨
          At times , people will simply not come through for you in the way you need.Forgive them and move on.
          posts - 212,comments - 87,trackbacks - 0
          • html

          無論是獨立的html,還是其他程序生成的,如servlet等,注意在最終的html的<head>和</head>之間必須加入meta標簽,用來指定html中輸入字符的編碼.如:
           <head>
           <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
           <title>測試GET && POST-Send</title>
           </head>

          • jsp和servlet

           首先必須解決程序輸出(如response.writeln(String s))和接受從客戶端傳來的數據(如request.getParameter(String sname))編碼問題,我們可以利用文件過濾功能,具體需要所用的jsp/servlet容器或者服務器提供的功能設置,如在Tomcat5.5.9中可以在webapps/yourAppDirectory/WEB-INF/web.xml中設置如下:

          <filter>
            <filter-name>SetCharsetEncodingFilter</filter-name>
            <display-name>SetCharsetEncodingFilter</display-name>
            <description>Set CharsetEncoding Filter</description>
            <filter-class>com.gg.comm.web.SetCharsetEncodingFilter</filter-class>
            <init-param>
             <param-name>encoding</param-name>
             <param-value>gb2312</param-value>
            </init-param>
          </filter>
          <filter-mapping>
            <filter-name>SetCharsetEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
          </filter-mapping>

          其中SetCharsetEncodingFilter Class就是用來設置request和reponse字符編碼的filter類,其中設置語句如下:

            request.setCharacterEncoding(targetEncoding);
            response.setContentType("text/html");
            response.setCharacterEncoding(targetEncoding);

           另外為了解決通過get(url中帶有參數)方式傳遞參數的亂碼問題,我們還需要設置一下url傳遞參數所需要的編碼,具體在Tomcat5.5.9中可以在${Tomcat_home}\conf\server.xml中的<connector>和</connector>之間設置,如下:

          <!--
           URIEncoding="GBK":Force GET method String(Chinese) can be transferd properly by http:uri

              note:Tomcat only support GBK specification,so not set charset gb2312
          -->
          <Connector URIEncoding="GBK" port="80" redirectPort="8443" maxSpareThreads="75"  maxThreads="150" minSpareThreads="25">
          </Connector>

           最后為了解決jsp的亂碼問題,我們還需要作如下處理,即在左右的jsp頭均加上如下指令:

            <%@ page contentType="text/html;charset=gb2312" language="java" %>
            或者
            <%@ page pageEncoding="gb2312"%>

          • jdbc和數據庫

          關于寫入數據庫和讀取數據庫數據的亂碼問題,可以通過如下方式輕松解決:

          • 對于JAVA程序的處理方法按我們指定的方法處理。
          • 把數據庫默認支持的編碼格式改為GBK或GB2312的。

          到此,一般來說對于WEB方式的應用來說,中文問題就可以解決了。當然以上方法是根據統一編碼的原則解決的以及WEB方式的文件轉換關系(file->class->load->execute or transfered or response or request)來做的。

          posted on 2005-06-14 10:25 瀟瀟雨 閱讀(226) 評論(0)  編輯  收藏 所屬分類: JAVA
          主站蜘蛛池模板: 高安市| 内江市| 固原市| 清徐县| 岗巴县| 安阳市| 洛扎县| 武陟县| 乐至县| 临西县| 延津县| 山东省| 重庆市| 沙河市| 阜康市| 简阳市| 夹江县| 新郑市| 静海县| 离岛区| 沅陵县| 河间市| 长寿区| 高尔夫| 凌云县| 和顺县| 陆川县| 中西区| 莱芜市| 剑河县| 呼图壁县| 江川县| 奇台县| 丹东市| 凤山县| 龙陵县| 佛冈县| 衡东县| 石城县| 玉山县| 铜陵市|