在Tomcat上运行的这个非常简单的Java应用程序中,是什么导致Chrome-IE和Firefox的不同行为?

在Tomcat上运行的这个非常简单的Java应用程序中,是什么导致Chrome-IE和Firefox的不同行为?,java,servlets,jakarta-ee,unicode,encoding,Java,Servlets,Jakarta Ee,Unicode,Encoding,我有一个在Tomcat8.5.4上运行的非常简单的Java web应用程序。以下是我在pom.xml中的依赖项 <dependencies> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3

我有一个在Tomcat8.5.4上运行的非常简单的Java web应用程序。以下是我在pom.xml中的依赖项

<dependencies>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
为什么Internet Explorer向我显示一些/utf16的中文文本,而不解析/utf32的HTML并将其显示为纯文本

小编辑.. 我尝试使用Jetty来确保该问题与Tomcat无关,并且所有浏览器上的所有输出都是相同的。

InternetExplorer有。Microsoft提供了一些解决方法,请尝试:

要解决此问题,请使用以下方法之一:

  • 禁用Internet Explorer中的自动选择设置
  • 在HTTP头中提供字符集
  • 将META标记移动到MSHTML解析的数据的第一个千字节内。虽然我们不知道解析器一次读取多少数据,但是这个位置将解决这个问题
  • 增加服务器初始HTTP响应的大小。初始大小应至少为1 KB
  • 确保系统区域设置与HTML页面中指定的元标记的字符集匹配
您的
也是HTML5方式。我无法检查IE11是否支持它,但是使用旧的
声明也值得一试

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
</web-app>
@WebServlet(urlPatterns = "/utf16")
public class UTF16 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-16");
        resp.setContentType("text/html");

        final PrintWriter writer = resp.getWriter();
        writer.print("<!doctype html>\n" +
                     "<html lang=\"en\">\n" +
                     "<head>\n" +
                         "<meta charset=\"utf-16\"/>");
        writer.print("</head>\n");
        writer.print("<body>");
        writer.print('\u20ac'); // This is the unicode code point for the Euro sign.
        writer.print("<body>");
        writer.print("</html>");
        writer.flush();
    }
}
@WebServlet(urlPatterns = "/utf32")
public class UTF32 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-32");
        resp.setContentType("text/html");

        final PrintWriter writer = resp.getWriter();
        writer.print("<!doctype html>\n" +
                     "<html lang=\"en\">\n" +
                     "<head>\n" +
                         "<meta charset=\"utf-32\"/>");
        writer.print("</head>\n");
        writer.print("<body>");
        writer.print('\u20ac'); // This is the unicode code point for the Euro sign.
        writer.print("<body>");
        writer.print("</html>");
        writer.flush();
    }
}
HTTP/1.1 200
Content-Type: text/html;charset=UTF-16
Transfer-Encoding: chunked
Date: Sat, 01 Apr 2017 12:57:48 GMT