Tomcat 8.0-mime映射&;内容类型

Tomcat 8.0-mime映射&;内容类型,tomcat,servlets,mime-types,content-type,tomcat8,Tomcat,Servlets,Mime Types,Content Type,Tomcat8,最近,我一直在深入研究一个问题,这个问题导致我在web-common_X_X.xsd中定义并在web-apps web.xml文件中使用了“mime映射”元素。我的目标是将Tomcat配置为在向特定servlet返回响应时包含特定的内容类型头 我发现提到了功能,但我无法得到一个使用Tomcat 8.0.33的简单示例 对于此测试,我创建了以下servlet: public class SimpleServlet extends HttpServlet { protected void d

最近,我一直在深入研究一个问题,这个问题导致我在web-common_X_X.xsd中定义并在web-apps web.xml文件中使用了“mime映射”元素。我的目标是将Tomcat配置为在向特定servlet返回响应时包含特定的内容类型头

我发现提到了功能,但我无法得到一个使用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>

单纯形
com.jamf.swa.SimpleServlet
单纯形
*斯瓦先生
西南航空
文本/rtf;字符集=UTF-8
我尝试了这个测试,在“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
curl--跟踪ascii-http://localhost:8080/testing.swa
==信息:正在尝试::1。。。
==信息:TCP\U节点延迟设置
==信息:已连接到本地主机(::1)端口8080(#0)
=>发送头,89字节(0x59)
0000:GET/testing.swa HTTP/1.1
001b:主机:本地主机:8080
0031:用户代理:curl/7.51.0
004a:接受:*/*
0057: 

根据Tomcat邮件列表,“mime映射”功能仅由Tomcat的DefaultServlet使用。其他servlet可以利用这种机制,但它必须显式内置

我所描述的功能将由一个过滤器来完成,我想这就是我要走的路线