??? ie默認根據服務器傳回來的contentType頭進行顯示(忽略mata標簽),對于html這種靜態文件,由于沒有contentType頭則根據<meta http-equiv="Content-Type" content="text/html; charset=GBK" />標簽中的編碼類型進行顯示.
??? pageEncoding指定的是jsp編譯時的編碼格式,必須對應于jsp文件內容的編碼,否則是亂碼
默認pageEncoding為:ISO-8859-1,如果不指定contentType,輸出對應于pageEncoding的編碼方式
也就是如果都不設置的話,默認輸出ISO-8859-1,肯定是亂碼(保存為unicode即可正常顯示).
??? response.setCharacterEncoding("GBK")
<%@ page contentType="text/html; charset=UTF-8">
這兩句作用相同,設置輸出的編碼類型,但response.setCharacterEncoding("GBK")優先級高
??? 通過 get/post 方式從 ie中發送漢字,發送編碼方式由Content-Type決定,request.getParameter("XX")得到的字符串是用ISO-8859-1表示的,所以必須在取值前用HttpServeletRequest.setCharacterEncoding 設置想得到的編碼類型,或是在<Connector>中添加URIEncoding="GBK"屬性,來獲取正確的編碼類型,但是,在執行setCharacterEncoding()之前,不能執行任何getParameter()。java doc上說明:This method must be called prior to reading request parameters or reading input using getReader()。而且,該指定只對POST方法有效,對GET方法無效。分析原因,應該是在執行第一個getParameter()的時候, java將會按照編碼分析所有的提交內容,而后續的getParameter()不再進行分析,所以setCharacterEncoding()無效。 而對于GET方法提交表單是,提交的內容在URL中,一開始就已經按照編碼分析所有的提交內容,setCharacterEncoding()自然就無效。
<%
@?page?contentType
=
"
text/html;?charset=UTF-8
"
?pageEncoding
=
"
UTF-8
"
%>
<%
request.setCharacterEncoding(
"
UTF-8
"
);
response.setCharacterEncoding(
"
UTF-8
"
);
%>
<
html
>
??
<
head
>
????
<
title
>
test
</
title
>
????
<
meta?
http-equiv
="Content-Type"
?content
="text/html;?charset=UTF-8"
?
/>
??
</
head
>
??
<
body
>
=
<%
=
new
?
String
(request.getParameter(
"
foo
"
).getBytes(
"
iso8859-1
"
),
"
UTF-8
"
)
%>
=
??????
<
form?
action
=""
?method
="get"
>
??????foo?=?
<
input?
type
="text"
?name
="foo"
?value
="${param["
foo"]}"
>
??????????
<
input?
type
="submit"
>
??????
</
form
>
??????tomcat:
<
Connector?
port
="8080"
?URIEncoding
="GBK"
?
/>
??
</
body
>
</
html
>
??? pageEncoding指定的是jsp編譯時的編碼格式,必須對應于jsp文件內容的編碼,否則是亂碼
默認pageEncoding為:ISO-8859-1,如果不指定contentType,輸出對應于pageEncoding的編碼方式
也就是如果都不設置的話,默認輸出ISO-8859-1,肯定是亂碼(保存為unicode即可正常顯示).
??? response.setCharacterEncoding("GBK")
<%@ page contentType="text/html; charset=UTF-8">
這兩句作用相同,設置輸出的編碼類型,但response.setCharacterEncoding("GBK")優先級高
??? 通過 get/post 方式從 ie中發送漢字,發送編碼方式由Content-Type決定,request.getParameter("XX")得到的字符串是用ISO-8859-1表示的,所以必須在取值前用HttpServeletRequest.setCharacterEncoding 設置想得到的編碼類型,或是在<Connector>中添加URIEncoding="GBK"屬性,來獲取正確的編碼類型,但是,在執行setCharacterEncoding()之前,不能執行任何getParameter()。java doc上說明:This method must be called prior to reading request parameters or reading input using getReader()。而且,該指定只對POST方法有效,對GET方法無效。分析原因,應該是在執行第一個getParameter()的時候, java將會按照編碼分析所有的提交內容,而后續的getParameter()不再進行分析,所以setCharacterEncoding()無效。 而對于GET方法提交表單是,提交的內容在URL中,一開始就已經按照編碼分析所有的提交內容,setCharacterEncoding()自然就無效。























