【问题标题】:Displaying UTF-8 text in a JSP page from a Servlet在 Servlet 的 JSP 页面中显示 UTF-8 文本
【发布时间】:2018-05-14 10:09:48
【问题描述】:

当我尝试通过 request.setAttribute() 在 JSP 上显示德语文本(例如 Zurücksetzen)时,它显示为 Zur�cksetzen

request.setAttribute("test", "Zurücksetzen");

我的 JSP 页面将 contentType 定义为 UTF-8:

<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

我只是用${test}显示属性。


如果我将请求转发到 JSP 页面而不是包含 JSP,则文本显示正确

转发(工作):
request.getRequestDispatcher("/WEB-INF/views/index.jsp").forward(request, response);

包括(无效):
request.getRequestDispatcher("/WEB-INF/views/index.jsp").include(request, response);


我的 IDE 使用的是 UTF-8

【问题讨论】:

  • 您的文件是否也使用 UTF-8 编码保存?
  • Zur�cksetzenISO-8859-1 数据正在(或尝试)显示在UTF-8 中的结果。你的 Java 文件是 UTF-8 的吗?
  • 如果我的 Java 文件是 UTF-8,我该如何解决?我在使用 IntelliJ 的 Macbook 上
  • 谁把问题记下来了!?原因?

标签: java jsp


【解决方案1】:

回答我自己的问题:

JSP Globalization Support中所述,默认如下

The default MIME type is text/html for traditional JSP pages; it is text/xml for JSP XML documents.

The default for the page source character encoding (for translation) is ISO-8859-1 (also known as Latin-1) for traditional JSP pages; it is UTF-8 or UTF-16 for JSP XML documents.

The default for the response character encoding is ISO-8859-1 for traditional JSP pages; it is UTF-8 or UTF-16 for JSP XML documents.

The determination of UTF-8 versus UTF-16 is according to "Autodetection of Character Encodings" in the XML specification, at the following location

所以ServletJSP 页面默认为ISO-8859-1

request.getRequestDispatcher("/WEB-INF/views/index.jsp").include(request, response);
当您.include 一个 JSP 页面时,如上所述,该页面使用默认字符编码 (ISO-8859-1) 进行编码。要使用 UTF-8 编码,您必须设置 response.setCharacterEncoding("UTF-8"); 注意:JSP 中的 ContentType 指令被忽略。

request.getRequestDispatcher("/WEB-INF/views/index.jsp").forward(request, response);
当您.forward 访问 JSP 页面时,如上所述,或直接从浏览器访问 JSP 页面,页面使用默认字符编码 (ISO-8859-1) 进行编码。为了使用 UTF-8 编码,您必须在 JSP 页面的第一行添加 &lt;%@ page contentType="text/html; charset=UTF-8" %&gt;

【讨论】:

    【解决方案2】:

    设置ServletResponse的编码:

    response.setCharacterEncoding("UTF-8");
    

    【讨论】:

    • 这行得通:)。但是您可能会因为您的简短回答(我喜欢)而丢分。很高兴!尝试了几个小时!谢谢
    • 这是“解决”这个问题的一种非常糟糕的方法。您无需在响应中显式设置字符编码。那将是非常乏味的。
    • @LuiggiMendoza 这就是为什么我实现一个抽象的 Servlet 并定义了这样的设置,然后从具体的 Servlet 实现。
    • 伙计们,a) 请在这里冷静下来(我看到一条冒犯性的评论在几秒钟后被删除)和 b) 那些可以提出更好解决方案的人可能会发布另一个问题的答案,对吧?无需宣传。
    • 这个问题有很多重复项。浏览这些并查看需要考虑的所有部分。字符编码比人们想象的要复杂得多,所以提出这样的解决方案会让人产生错误的印象。手动设置响应编码不是解决方案,而是一种 hack。
    【解决方案3】:

    正如 here 使用 Spring 解释的那样,最好的方法是使用 Spring 过滤器 CharacterEncodingFilter 在 web.xml 中全局设置对 UTF-8 的响应:

    <filter>  
        <filter-name>encodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
           <param-name>encoding</param-name>  
           <param-value>UTF-8</param-value>  
        </init-param>  
        <init-param>  
           <param-name>forceEncoding</param-name>  
           <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>encodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-22
      • 2013-11-25
      • 2012-09-25
      • 2012-12-01
      • 2011-07-27
      • 2010-12-22
      • 1970-01-01
      相关资源
      最近更新 更多