java 将两个不同的 servlet 映射到相同的 URL 模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7885666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:50:36  来源:igfitidea点击:

Having two different servlets mapped on the same URL pattern

javaservletsweb.xml

提问by kaiwii ho

I encountered a J2EE project written by others. When I come to the web.xml, there are two different servlets mapped on the same URL pattern. I wonder the purpose of this approach. How exactly does that work and what's the puspose?

遇到一个别人写的J2EE项目。当我来到 时web.xml,有两个不同的 servlet 映射到相同的 URL 模式。我想知道这种方法的目的。这究竟是如何工作的,目的是什么?

Here is the relevant part of the web.xml:

这是 的相关部分web.xml

<servlet>
    <servlet-name>fileDownload</servlet-name>
    <servlet-class>com.htsoft.core.web.servlet.FileDownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>fileDownload</servlet-name>
    <url-pattern>/file-download</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>fileDownLoad</servlet-name>
    <servlet-class>com.kaiwii.oa.action.system.FileDownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>fileDownLoad</servlet-name>
    <url-pattern>/file-downLoad</url-pattern>
</servlet-mapping>  

回答by Dave Newton

Only one servlet will get called; there's no mechanism I'm aware of for handling a single request with two servlets (nor am I sure what that would even mean).

只有一个 servlet 会被调用;我不知道用两个 servlet 处理单个请求的机制(我也不确定这意味着什么)。

Servlet URL patterns may overlap, but having two with the exact same URL doesn't make sense. I don't recall if the servlet spec explicitly disallowsit, however matching stops at the first matching. The matching method is defined in the spec.

Servlet URL 模式可能会重叠,但有两个具有完全相同的 URL 没有意义。我不记得 servlet 规范是否明确禁止它,但是匹配在第一次匹配时停止。匹配方法在规范中定义。

Servlet 2.4 spec PDFSee p. 85+

Servlet 2.4 规范 PDF参见第 10 页。85+