tomcat5中,因?yàn)楸娝苤脑颍瑸榱吮WCget/post數(shù)據(jù)都采用相同的UTF8編碼,我們在server.xml中進(jìn)行了如下設(shè)置:
maxSpareThreads="75" enableLookups="false" redirectPort="8443"
acceptCount="100" debug="99" connectionTimeout="20000"
disableUploadTimeout="true" URIEncoding="UTF-8"/>
這里指定了get時(shí)候的數(shù)據(jù)編碼。但是,當(dāng)使用IIS作為webserver轉(zhuǎn)發(fā)servlet/jsp請求給Tomcat時(shí)候,這個(gè)設(shè)置卻失效了。其實(shí)原因很簡單:IIS是通過AJP協(xié)議,把請求轉(zhuǎn)發(fā)到Tomcat監(jiān)聽的8009端口上的,所以這里針對8080的設(shè)置自然就無效了。正確的方法是進(jìn)行下面的設(shè)置:
debug="0" protocol="AJP/1.3" URIEncoding="UTF-8"/>
雖然是小問題,卻花了我?guī)讉€(gè)小時(shí)才想到。
一、請求結(jié)果的亂碼:
解決辦法:在顯示中文字符串前加上 request.setCharacterEncoding("gbk");
或者:在獲取字符串str后使用str = new String(str.getBytes("ISO-8859-1"),"GB2312");轉(zhuǎn)換
ISO-8859-1是默認(rèn)的字符編碼
解決辦法:
1、項(xiàng)目已打包到web服務(wù)器:
找到 %TOMCAT_HOME%\conf\server.xml文件,將此文件的代碼段末尾加 入 URIEncoding="gbk",結(jié)果如下所示:
<Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="gbk"/>
2、開發(fā)過程中:
找到 %project_name%\Tomcat\conf\server.xml文件,刪除文件中的備注,增加URIEncoding="gbk",結(jié)果如下所示:
<Connector acceptCount="10" connectionTimeout="60000" maxThreads="75" minSpareThreads="5" port="8080" URIEncoding="gbk"/>
二、處理響應(yīng)結(jié)果的亂碼..
1.在servlet中 response.setContentType("text/html;charset=GB2312");
2.在jsp中 <%@page contentType="text/html;charset=GB2312"%>
3.在html中 <head><META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=GB2312"></head>
一、提交中文是使用javascript的函數(shù):encodeURIComponent()進(jìn)行編碼,
例如:
String
param=encodeURIComponet("param");
然后到服務(wù)器端使用UTF-8編碼提取參數(shù):
request.setCharacterEncoding("UTF-8");
String str = request.getParameter("param");
然后使用下面的方式進(jìn)行解碼:
java.net.URLDecoder.decode(str,"UTF-8")
這樣就可以得到正確的參數(shù)。
<%
'先下載遠(yuǎn)程圖片
url="
http://www.baidu.com/img/logo.gif" '遠(yuǎn)程圖片地址
savepath="e:\www\www9551cn" '保存路徑
'給文件重命名
randomize
ranNum=int(999*rnd)
filename=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum
'文件重命名結(jié)束
set xmlhttp=server.createobject("Microsoft.XMLHTTP")
xmlhttp.open "get",url,false
xmlhttp.send
img = xmlhttp.ResponseBody
text=xmlhttp.ResponseText
set xmlhttp=nothing
set objAdostream=server.createobject("ADODB.Stream")
objAdostream.Open()
objAdostream.type=1
objAdostream.Write(img)
objAdostream.SaveToFile(savepath&filename&".jpg")
objAdostream.SetEOS
set objAdostream=nothing
'文件下載結(jié)束
%>
在JSP的查詢或者插入數(shù)據(jù)庫的時(shí)候總會(huì)遇到亂碼的問題,下面給出兩種解決方案:
1、在頁面中加上 request.setCharacterEncoding("utf-8");
2、String name=new String(request.getparameters("name").getBytes("ISO8859_1"),"utf-8")