Tomcat 8.0 - mime 映射和内容类型

标签 tomcat servlets mime-types content-type tomcat8

最近我一直在深入研究一个问题,该问题导致我在 web-common_X_X.xsd 中定义并在 Web 应用程序 web.xml 文件中使用的“mime-mapping”元素。我的目标是将 Tomcat 配置为在返回对特定 servlet 的响应时包含特定的 Content-Type header 。

我找到了 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 header 。

我可以确认 StandardContext:addMimeMapping()使用我的映射正确调用,但我从未看到在我的请求期间读取 mimeMappings 局部变量。

我也没有在 Tomcat 方面找到任何关于在 v8+ 中使用此元素的文档。

我是否遗漏了一些微不足道的东西? Tomcat 是否不再支持此功能?如果您能提供任何帮助,我们将不胜感激。

最佳答案

根据 Tomcat 邮件列表,“mime-mapping”功能仅由 Tomcat 的 DefaultServlet 使用。其他 servlet 可以利用此机制,但必须显式内​​置。

我所描述的功能将由过滤器来完成,我认为这就是我要走的路。

关于Tomcat 8.0 - mime 映射和内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42261607/

相关文章:

spring - Tomcat - 子容器在启动期间失败 - 在将 maven 转换为 gradle 之后

java - 这是一个很好的做法和选择 :Sending a Hash Map object or a JSON from java servlet to jsp?

c - 在 C 程序中包含 UNIX 实用程序 'file'

jquery - json 问题(使用 zend 框架评估 ajax uploader )

java - 将servlet请求转发到java中的.html文件?

java - 是否有使用播客应用程序订阅提要的标准方法?

http - 在 https 上运行的生产服务器中,但在实时(在 Internet 上)中它仅显示 http

java - 无法连接到本地芝麻本地商店

java - 从websphere迁移到tomcat后无法访问wsdl文件

java - 在 heroku 上部署 tomcat servlet 项目