Java Servlet和jsp字符编码特性
我有一个需要显示unicode字符的Web应用程序。当我在jsp中编写字符串时,一切都很好,例如:Java Servlet和jsp字符编码特性,java,jsp,servlets,character-encoding,Java,Jsp,Servlets,Character Encoding,我有一个需要显示unicode字符的Web应用程序。当我在jsp中编写字符串时,一切都很好,例如: <%@ page contentType="text/html;charset=UTF-8" %> <%@ page import="com.xyz.foo.ConsoleApp" %> <html> <head> <meta charset="UTF-8"/> </head> <
<%@ 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>
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>");
}
使用
javac-encoding
参数告诉javac您的java源代码存储在什么编码中,否则它使用的是平台默认值,显然不是UTF-8。web应用服务器将编译.jsp文件。如果Web应用服务器支持,您可能还需要检查其编码。是的,但是jsp文件使用页面标题中的pageEncoding属性(如果存在),否则使用contentType属性的字符集值,在本例中指定为UTF-8.+1。这显然是编译器编码的问题。当编译器编码为Mac OS Roman时,源代码存储在UTF-8中,因此u
->C3 BA
->√∫代码>。这是有道理的,因为只有编译过的东西才有错误。我目前不在家,但我会尽快测试。
毫无意义,而且什么也不做,因为您已经在内容类型http头中发送了编码,因此会造成混乱和危害。移除它。
<%@ 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>
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