Servlet 和 jsp 字符编码特性

发布于 2024-09-30 23:25:19 字数 1500 浏览 4 评论 0原文

我有一个网络应用程序,我需要在其上显示 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对不⑦ 2024-10-07 23:25:19

使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文