2012-01-27 107 views
0

我有支持/ {servlet} /歷史的需求,並且有許多servlet需要支持此功能。我正在使用Tomcat,FWIW。我可以在Servlet映射中組合這些url模式嗎?

以下工作,但我想知道是否有一種方法可以將所有模式合併到一行,並避免爲每個需要支持歷史模式的servlet添加url模式。我試過了幾個選項,但都失敗了。

<servlet> 
    <servlet-name>History</servlet-name> 
    <servlet-class>com.foo.HistoryServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>History</servlet-name> 
    <url-pattern>/aDifferentServlet/history/*</url-pattern> 
    <url-pattern>/someOtherOne/history/*</url-pattern> 
    <url-pattern>/anotherExample/history/*</url-pattern> 
</servlet-mapping> 
... 
<servlet> 
    <servlet-name>aDifferentServlet</servlet-name> 
    <servlet-class>com.foo.aDifferentServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>aDifferentServlet</servlet-name> 
    <url-pattern>/aDifferentServlet/*</url-pattern> 
</servlet-mapping> 
... 

謝謝。

+0

你是什麼意思,「對於每個servlet」?您將單個servlet映射到多個路徑。 – 2012-01-27 16:05:02

+0

對不起,我指的是url-patterns中的servlet *。每條路徑都代表一個servlet。對於servletA,我需要支持通過servletA,B,C等獲取所有內容的歷史記錄。 – 2012-01-27 16:09:40

回答

3

爲了只有一個URL模式,你需要指定一個共同的前綴(文件夾)格式,如:/history/*或後綴(擴展)像*.history的模式。您不能在兩側使用通配符匹配的網址格式,例如*/history/*。最好的辦法是將歷史小服務程序映射到/history/*,並相應地將URL更改爲例如/history/aDifferentServlet(此部分可在歷史小服務程序中使用request.getPathInfo())。

如果更改URL是不可取的,那麼只要請求URI與*/history/*模式匹配,就需要創建Filter或重寫它們轉發給歷史servlet的servlet。

0

模式可以以星號結尾或以一個開頭(表示文件擴展名映射)。

更多信息在:

http://javapapers.com/servlet/what-is-servlet-mapping/#&slider1=1

The url-pattern specification: 

     *A string beginning with a ‘/’ character and ending with a ‘/*’ 
     suffix is used for path mapping. 
     *A string beginning with a ‘*.’ prefix is used as an extension mapping. 
     *A string containing only the ’/’ character indicates the "default" 
     servlet of the application. In this case the 
     servlet path is the request URI minus the context path and the path 
     info is null. 
     *All other strings are used for exact matches only. 
+0

對不起,我應該更清楚了。實際的servlet名稱不是servlet *,只是爲了掩蓋真實姓名。想更像/ doStuff,/ aDifferentServlet,/登錄等。 – 2012-01-27 16:11:58

+0

像上面這樣?編輯映射。你總是一個歷史,這是共同的部分權利? – fmucar 2012-01-27 16:33:58

+0

感謝您的幫助,但這似乎並不奏效。 – 2012-01-27 16:40:44

相關問題