what if url pattern matches multiple servlets?

2019-01-11 14:40发布

问题:

<servlet-mapping>
  <servlet-name> s1</servlet-name>
  <url-pattern> /abc </url-pattern>
</servlet-mapping>

<servlet-mapping>
  <servlet-name> s2</servlet-name>
  <url-pattern> /abc </url-pattern>
</servlet-mapping>

<servlet-mapping>
  <servlet-name> s3</servlet-name>
  <url-pattern> /* </url-pattern>
</servlet-mapping>

Which servlet will be called if a request /abc comes?and why?

回答1:

Check this. In short:

  • if the mappings have exactly the same pattern, there is no guarantee which servlet one will be invoked. So avoid that.
  • If the patterns are overlapping, the most specific one is picked. (/abc is more specific than /*)