Servlet 和 jsp 字符编码特性
我有一个网络应用程序,我需要在其上显示 unicode 字符。当我在 jsp 中编写字符串时一切都很好,例如:
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="com.xyz.foo.ConsoleApp" %>
<html>
<head>
<meta charset="UTF-8"/>
</head>
<body><%= "Setúbal" %></body>
</html>
我得到了所需的输出: Setúbal
但是,servlet 中的等效代码无法正确呈现,例如:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter writer = response.getWriter();
writer.println("<html>");
writer.println("<head><meta charset='UTF-8'/></head>");
writer.println("<body>Setúbal</body>");
writer.println("</html>");
}
当在 jsp 中,我从类加载文本:
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="com.xyz.foo.ConsoleApp" %>
<html>
<head>
<meta charset="UTF-8"/>
</head>
<body><%= ConsoleApp.getText() %></body>
</html>
在这两种情况下,我都会得到奇怪的字符: Set√∫bal
所有文件均为 UTF-8,响应标头具有以下内容:
Content-Type text/html; charset=utf-8
Content-Encoding gzip
Date Tue, 09 Nov 2010 09:44:05 GMT
Server Google Frontend
Cache-Control private, x-gzip-ok=""
Content-Length 438
I have a webapp on which I need to display unicode characters. It's all fine when I write the strings in the jsp, for example:
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="com.xyz.foo.ConsoleApp" %>
<html>
<head>
<meta charset="UTF-8"/>
</head>
<body><%= "Setúbal" %></body>
</html>
I get the desired output: Setúbal
However, the equivalent code in a servlet does not render properly, for example:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter writer = response.getWriter();
writer.println("<html>");
writer.println("<head><meta charset='UTF-8'/></head>");
writer.println("<body>Setúbal</body>");
writer.println("</html>");
}
The same thing happens when in the jsp I load the text from a class:
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="com.xyz.foo.ConsoleApp" %>
<html>
<head>
<meta charset="UTF-8"/>
</head>
<body><%= ConsoleApp.getText() %></body>
</html>
In both cases I get strange characters: Set√∫bal
All files are UTF-8 and the response headers have the following:
Content-Type text/html; charset=utf-8
Content-Encoding gzip
Date Tue, 09 Nov 2010 09:44:05 GMT
Server Google Frontend
Cache-Control private, x-gzip-ok=""
Content-Length 438
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 javac -encoding 参数告诉 javac 你的 java 源代码存储在什么编码中,否则它使用平台默认值,显然不是 UTF-8。
Use the
javac -encoding
parameter to tell javac what encoding your java source is stored in, otherwise it uses the platform default which apparently isn't UTF-8.