Tomcat 4.0和Tomcat 4.1下JSP頁面中文問題解答
目前, Tomcat 作為一種出色的開放源代碼的 JSP 服務器,目前在 JSP 的開發(fā)過程中獲得了廣泛的應用. 但是作為一款英語國家公司開發(fā)的軟件, 在中文環(huán)境下不可避免的會出現(xiàn)一些亂碼問題. 這里就 Tomcat 4.0和Tomcat 4.1 下的常見中文問題及其解決方法做一個總結(jié). 這些方法都已經(jīng)在 中文版 Windows 98 + JDK 1.3.1 和 中文版 Windows 2000 + JDK 1.3.1 下通過了測試. 另外在 IBM 的網(wǎng)站上有一個網(wǎng)頁 http://www-900.ibm.com/developerWorks/cn/java/jsp_dbcsz/index.shtml 也討論了這個問題.
首先為了便于討論, 這里首先列出了一些方便的工具方法, 便于我們的討論. 這些方法如下所示:
- public String toChi(String input) {
- try {
- byte[] bytes = input.getBytes("ISO8859-1");
- return new String(bytes);
- }catch(Exception ex) {
- }
- return null;
- }
- // 對給定字符進行 URL 編碼
- public String encode(String value) {
- if(isEmpty(value)) return "";
- return java.net.URLEncoder.encode(value);
- }
- // 對給定字符進行 URL 解碼
- public String decode(String value) {
- if(isEmpty(value)) return "";
- return java.net.URLDecoder.decode(value);
- }
問題1. 瀏覽器中看到的 JSP頁面中的漢字怎么都成了
可能原因如下: 您的頁面中沒有指定頁面的字符集為中文. 解決方法(適用于Tomcat 4.0和Tomcat 4.1)是在頁面中添加如下代碼:
- <%@ page contentType="text/html;charset=gb2312" %>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
問題2. 通過 POST 方法提交的表單的漢字都顯示為亂碼(在 Tomcat 4.0 下正常, Tomcat 4.1 下出現(xiàn)).
可能原因如下: POST 提交的字符串都是 ISO8859-1 編碼的, 只要把它的字符集轉(zhuǎn)換到中文就行了. 解決方法如下(適用于 Tomcat 4.1):
- // 單個的參數(shù)
- String result = toChi(request.getParameter("parameterName"));
- // 多個參數(shù)
- String values[] = request.getParameterValues(name);
- if(values != null) {
- for(int i = 0; i < values.length; i++) {
- values[i] = toChi(values[i]);
- }
- }
問題3. 通過 GET 方法提交的表單的漢字都顯示為亂碼(在 Tomcat 4.0和Tomcat 4.1 下都出現(xiàn)).可能原因如下: GET 提交的字符串都是 ISO8859-1 編碼的, 只要把它的字符集轉(zhuǎn)換到中文就行了. 解決方法如下(適用于 Tomcat 4.1, Tomcat 4.0 下不能用于 page.jsp?username=中文):
- // 單個的參數(shù)
- String result = toChi(request.getParameter("parameterName"));
- // 多個參數(shù)
- String values[] = request.getParameterValues(name);
- if(values != null) {
- for(int i = 0; i < values.length; i++) {
- values[i] = toChi(values[i]);
- }
- }
問題4. Cookie 中不能寫入漢字或者漢字無法正確顯示.可能原因如下: Tomcat 4.0 下自動把 Cookie 做了編碼為 ISO8859-1 的存儲, 而 Tomcat 4.1 下的 JSP 引擎不支持包含含有漢字的 Cookie.
Tomcat 4.0 下的解決方法:
- // 根據(jù) Cookie 名稱得到請求中的 Cookie 值, 如果 Cookie 值是 null, 則返回 ""
- public String getCookieValue(HttpServletRequest request, String name) {
- Cookie[] cookies = request.getCookies();
- if(cookies == null) return "";
- for(int i = 0; i < cookies.length; i++) {
- Cookie cookie = cookies[i];
- if(cookie.getName().equals(name)) {
- // 需要對 Cookie 中的漢字進行 URL 反編碼, 適用版本: Tomcat 4.0
- return decode(cookie.getValue());
- }
- }
- // A cookie might not return a null value, may return a ""
- return "";
- }
Tomcat 4.1 下的解決方法:
- // 寫入包含漢字 Cookie 的方法
- response.addCookie(new Cookie("cookieName", encode("漢字")));
- // 得到 Cookie 值的方法(同 Tomcat 4.0 的解決方法)
- public String getCookieValue(HttpServletRequest request, String name) {
- Cookie[] cookies = request.getCookies();
- if(cookies == null) return "";
- for(int i = 0; i < cookies.length; i++) {
- Cookie cookie = cookies[i];
- if(cookie.getName().equals(name)) {
- // 需要對 Cookie 中的漢字進行 URL 反編碼, 適用版本: Tomcat 4.0
- return decode(cookie.getValue());
- }
- }
- // A cookie might not return a null value, may return a ""
- return "";
- }
問題5. 在 Tomcat 4.0 下 GET 請求(如: page.jsp?username=中文) 無法返回原來的值.原因: 與 Tomcat 引擎有關(guān), 不論是否轉(zhuǎn)換內(nèi)碼, 均無法返回原來的值, 但是有一個替代方法, 如下:
將URL地址改變?yōu)?"page.jsp?username=" + encode("中文")然后使用下列代碼取回參數(shù):
- // 單個的參數(shù)
- String result = toChi(request.getParameter("parameterName"));
【編輯推薦】