Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having two different servlets mapped on the same URL pattern

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?

Here is the relevant part of the 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>  
like image 520
kaiwii ho Avatar asked Oct 25 '11 06:10

kaiwii ho


1 Answers

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 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 disallows it, however matching stops at the first matching. The matching method is defined in the spec.

Servlet 2.4 spec PDF See p. 85+

like image 75
Dave Newton Avatar answered Oct 28 '22 23:10

Dave Newton