我创建了以下 jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<%
byte[] oe1 = {-61,-123};
byte[] oe2 = {-123,-61};
byte[] oe3 = "œ".getBytes("UTF-8");
%>
byte[] oe1 = {-61,-123}: '<%=new String(oe1, "UTF-8")%>'<br/>
byte[] oe2 = {-123,-61}: '<%=new String(oe2, "UTF-8")%>'<br/>
byte[] oe3 = "œ".getBytes("UTF-8"): '<%=new String(oe3, "UTF-8")%>'<br/>
oe3[0], oe3[1]: <%=oe3[0]%>, <%=oe3[1]%>
</body>
</html>
打印出以下内容:
byte[] oe1 = {-61,-123}: '�'
byte[] oe2 = {-123,-61}: '??'
byte[] oe3 = "œ".getBytes("UTF-8"): 'œ'
oe3[0], oe3[1]: -61, -123
我在这里错过了什么。为什么 oe3 有效,但 oe1 或 oe2 无效。这里可能有一些我不理解的编码问题
最佳答案
将它添加到 JSP 的顶部,让它使用 UTF-8 打印字符,并让浏览器将响应解释为 UTF-8。
<%@ page pageEncoding="UTF-8" %>
<meta>
标签不会那样做。更重要的是,当通过 HTTP 提供页面时,它会被忽略。
另见:
关于java - UTF8 和 jsp,试图了解发生了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6253402/