-------------------
Tomcat 5中文問題
author:[email protected]
-------------------
問題描述:
1 表單提交的數據,用request.getParameter(“xxx”)返回的字符串為亂碼或者??
2 直接通過url如http://localhost/a.jsp?name=中國,這樣的get請求在服務端用request. getParameter(“name”)時返回的是亂碼;按tomcat4的做法設置Filter也沒有用或者用request.setCharacterEncoding("GBK");也不管用
原因:
1 tomcat的j2ee實現對表單提交即post方式提示時處理參數采用缺省的iso-8859-1來處理
2 tomcat對get方式提交的請求對query-string 處理時采用了和post方法不一樣的處理方式。(與tomcat4不一樣,所以設置setCharacterEncoding(“gbk”))不起作用。
解決辦法:
首先所有的jsp文件都加上:
<SCRIPT language=SERVERASP><!--@ page contentType="text/html;charset=gb2312"--></SCRIPT>
1 實現一個Filter.設置處理字符集為GBK。(在tomcat的webapps/servlet-examples目錄有一個完整的例子。請參考web.XML和SetCharacterEncodingFilter的配置。)
1)只要把%TOMCAT安裝目錄%/ webappsservlets-examplesWEB-INFclassesfiltersSetCharacterEncodingFilter.class文件拷到你的webapp目錄/filters下,假如沒有filters目錄,就創建一個。
2)在你的web.xml裡加入如下幾行:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<Connector
port="80" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true"
URIEncoding="GBK"
/>
<%@ page contentType="text/Html;charset=gb2312"%>
<%@ page import="Java.util.*"%>
<%
String q=request.getParameter("q");
q = q == null? "沒有值" : q;
%>
<HTML>
<HEAD><TITLE>新聞列表顯示</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=pragma content=no-cache>
<body>
你提交了:
<%=q%>
<br>
<form action="tcnchar.jsp" method="post">
輸入中文:<input type="text" name="q"><input type="submit" value="確定">