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()自然就无效。

<%
@ 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
>

本文解析了不同场景下设置字符编码的方法,包括页面编码、请求参数编码及响应编码等关键配置,确保跨平台正确显示。

1254

被折叠的 条评论
为什么被折叠?



