JSP输出编码
当我编写以下内容时
é
,甚至
<%=new String(new byte[]{(byte) 0xC3, (byte) 0xA9}, "UTF-8")%>
得到一个长度为1的字符串。它包含单个UTF-8字符C3A9
或é
。
但是当它被写入浏览器时,浏览器无法解码它,除非我使用 latin1。因此,这意味着即使在 JSP 的顶部我有
<%@page import="..." contentType="text/html" pageEncoding="UTF-8" session="false"%>
如何让 JSP 输出 UTF-8 而不是编写 UTF-8 标头然后使用 latin1 进行编码,它也会被编码和写入为西方格式? “西方 (ISO-8859-1)”
When I write the following
é
or even
<%=new String(new byte[]{(byte) 0xC3, (byte) 0xA9}, "UTF-8")%>
I get a string with the length of 1. It contains a single UTF-8 character C3A9
or é
.
But when it gets written to the browser, the browser cannot decode it unless I use latin1. So that means it is getting encoded and written as western even though in the top of the JSP I have
<%@page import="..." contentType="text/html" pageEncoding="UTF-8" session="false"%>
How can I get the JSP to output UTF-8 instead of writing a UTF-8 header and then encoding with latin1? "Western (ISO-8859-1)"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该只使用
pageEncoding
,并编写符号本身,而不是通过其代码引用它。另请确保您的
.jsp
文件也采用 UTF-8 编码。您的 IDE 应处理pageEncoding
属性并适当设置编码,但通过特定设置或者如果不使用 IDE,.jsp
仍可以使用ISO 进行编码-8859-1
。将其更改为UTF-8
最后,确保您的响应确实使用
UTF-8
作为编码(例如使用 firebug)。如果没有,请尝试设置response.setCharacterEncoding(..)
You should only use
pageEncoding
, and write the symbol itself, rather then referring to it by its code.Also make sure your
.jsp
file is also encoded in UTF-8. Your IDE should handle thepageEncoding
attribute and set the encoding appropriately, but with particular settings or if not using an IDE, the.jsp
can still be encoded inISO-8859-1
. Change that toUTF-8
Finally, make sure your response is really using
UTF-8
as encoding (use firebug for example). If not, try setting itresponse.setCharacterEncoding(..)