【发布时间】:2017-07-04 20:03:24
【问题描述】:
最近我一直在研究一个问题,该问题导致我在 web-common_X_X.xsd 中定义并在 web 应用程序 web.xml 文件中使用的“mime-mapping”元素。我的目标是配置 Tomcat 在返回对特定 servlet 的响应时包含特定的 Content-Type 标头。
我发现 previous stack overflow posts 提到了该功能,但我无法找到一个使用 Tomcat 8.0.33 的简单示例。
对于这个测试,我创建了以下 servlet:
public class SimpleServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
IOUtils.write("Hello There!", resp.getOutputStream());
resp.setStatus(202);
}
}
并且有以下web.xml:
<web-app
version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>SimpleServlet</servlet-name>
<servlet-class>com.jamf.swa.SimpleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimpleServlet</servlet-name>
<url-pattern>*.swa</url-pattern>
</servlet-mapping>
<mime-mapping>
<extension>swa</extension>
<mime-type>text/rtf;charset=UTF-8</mime-type>
</mime-mapping>
</web-app>
我已经尝试过这个测试,无论是否包含“mime-type”元素中包含的字符集。 “text/rtf”类型也是任意的。我测试过其他人。
应用程序启动后,我向 /testing.swa 发出以下请求:
curl --trace-ascii - http://localhost:8080/testing.swa
== Info: Trying ::1...
== Info: TCP_NODELAY set
== Info: Connected to localhost (::1) port 8080 (#0)
=> Send header, 89 bytes (0x59)
0000: GET /testing.swa HTTP/1.1
001b: Host: localhost:8080
0031: User-Agent: curl/7.51.0
004a: Accept: */*
0057:
<= Recv header, 23 bytes (0x17)
0000: HTTP/1.1 202 Accepted
<= Recv header, 27 bytes (0x1b)
0000: Server: Apache-Coyote/1.1
<= Recv header, 20 bytes (0x14)
0000: Content-Length: 12
<= Recv header, 37 bytes (0x25)
0000: Date: Wed, 15 Feb 2017 22:37:17 GMT
<= Recv header, 2 bytes (0x2)
0000:
<= Recv data, 12 bytes (0xc)
0000: Hello There!
Hello There!== Info: Curl_http_done: called premature == 0
== Info: Connection #0 to host localhost left intact
如您所见,没有包含 Content-Type 标头。
我可以确认 StandardContext:addMimeMapping() 被我的映射正确调用,但我从未看到在我的请求期间读取 mimeMappings 局部变量。
我也没有在 Tomcat 方面找到任何文档以将此元素与 v8+ 一起使用。
我错过了一些微不足道的事情吗? Tomcat 是否已不再支持此功能?您能提供的任何帮助将不胜感激。
【问题讨论】:
标签: tomcat servlets mime-types content-type tomcat8