2017-02-15 47 views
4

最近我一直在深入研究一個問題,它引出了web-common_X_X.xsd中定義的「mime-mapping」元素,並將其用於web應用程序web.xml文件。我的目標是將Tomcat配置爲在向特定的Servlet返回響應時包含特定的Content-Type頭。Tomcat 8.0 - mime-mapping&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」元素中。 「文本/ 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是否已經不再支持這種功能?任何幫助,你可以提供將不勝感激。

回答

2

根據Tomcat郵件列表,「mime-mapping」功能僅由Tomcat的DefaultServlet使用。其他servlet可以利用這種機制,但必須明確內置。

我所描述的功能將通過過濾器來完成,我認爲這是我將要採用的路線。

相關問題