無論是獨立的html,還是其他程序生成的,如servlet等,注意在最終的html的<head>和</head>之間必須加入meta標簽,用來指定html中輸入字符的編碼.如:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>測試GET && POST-Send</title>
</head>
首先必須解決程序輸出(如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"%>
關于寫入數據庫和讀取數據庫數據的亂碼問題,可以通過如下方式輕松解決:
- 對于JAVA程序的處理方法按我們指定的方法處理。
- 把數據庫默認支持的編碼格式改為GBK或GB2312的。
到此,一般來說對于WEB方式的應用來說,中文問題就可以解決了。當然以上方法是根據統一編碼的原則解決的以及WEB方式的文件轉換關系(file->class->load->execute or transfered or response or request)來做的。
posted on 2005-06-14 10:25
瀟瀟雨 閱讀(221)
評論(0) 編輯 收藏 所屬分類:
JAVA