【发布时间】:2012-04-22 06:18:13
【问题描述】:
我正在从 Java 创建 test.js,如下所示。 Test.js 实现了函数 d(),接收作为参数的特殊字符 ∼ ('\u0098');
函数 d() 应该显示此特殊字符的 charCodeAt(),即 152。但是,它显示 732。
请注意,字符 152 和 732 都由特殊字符 ~ 表示,如下所示。
http://www.fileformat.info/info/unicode/char/098/index.htm
http://www.fileformat.info/info/unicode/char/2dc/index.htm
如何强制函数 d() 显示 152 而不是 732? (字符集问题?)。谢谢
TEST.JAVA
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setHeader("Content-Type", "text/javascript;charset=ISO-8859-1");
res.setHeader("Content-Disposition","attachment;filename=test.js");
res.setCharacterEncoding("ISO-8859-1");
PrintWriter printer=res.getWriter();
printer.write("function d(a){a=(a+\"\").split(\"\");alert(a[0].charCodeAt(0));};d(\""); // Writes beginning of d() function
printer.write('\u0098'); // Writes special character as parameter of d()
printer.write("\");"); // Writes end of d() function
printer.close();
}
由 TEST.JAVA 创建的 TEST.JS
function d(a)
{
a=(a+"").split("");
alert(a[0].charCodeAt(0));
};
d("˜"); // Note special character representing '\u0098'
TEST.HTML
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
<body>
<script type="text/javascript" charset="ISO-8859-1" src="test.js"></script>
</body>
</html>
【问题讨论】:
-
这是stackoverflow.com/questions/10080605/… 的变体,反映了对字符和编码的相同误解,不包含合理的问题。
标签: javascript servlets unicode character-encoding iso-8859-1