Servlet and jsp character encoding idiosyncrasy
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 strang开发者_如何转开发e 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
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.
精彩评论