Tomcat – why *.jsp url pattern in web.xml does not work

jakarta-eeservletstomcatweb.xml

  1. I wanna map all request to the TestHandler Servlet, so I use /* pattern.
  2. Then I wanna exclude jsp mappings, so I add *.jsp pattern mapping to jsp in front of /*.
  3. Problem: .jsp doesn't catch the url http://localhost/project/fun.jsp at all. In stead, / pattern catches it. Why? How can this happen ?

<servlet-mapping>
  <servlet-name>jsp</servlet-name>
  <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

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

But if I use a certain url-pattern like fun.jsp instead of *.jsp, it works. The fun.jsp pattern catch the url above. Who can tell me why ?

Best Answer

The patterns ending with /* (path rules) are matched before the *. starting (extension rules) mappings. The exact URI is an exact match, which is the 1st in the evaluation order.

Set TestHandler as default servlet, that should work.

<servlet-mapping>
  <servlet-name>TestHandler</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>
Related Topic