Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what if url pattern matches multiple servlets?

<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?

like image 527
Xyzxyz Xyz Avatar asked Oct 29 '11 09:10

Xyzxyz Xyz


People also ask

Can a servlet have multiple url patterns?

If the web. xml file contains two identical mappings to different servlets, the container makes no guarantees about which servlet the container calls for a given request. However, two servlets may use overlapping url-pattern elements.

Can you have multiple servlets?

You can declare multiple servlets using the same class with different initialization parameters.


1 Answers

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 /*)
like image 164
Bozho Avatar answered Oct 08 '22 08:10

Bozho