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

0

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

5 Comments

so, How can I change the order?
you can't, this is the servlet specification
You should try using / (and not /*) as url-pattern, which is the default servlet
Looks like / works very well. Thank you very much! But I don't understand the difference between / and /*! And where can find the documentation of this part (the url-pattern) ?
/ is THE default mapping, which is different from the /* ending mappings! This page explains it quite well: javapapers.com/servlet/what-is-servlet-mapping For the exact specification you have to check the Java Servlet specification I'm afraid...
1

To write a servlet mapping there should a servlet class defined.

<servlet>
    <servlet-name>TestHandler</servlet-name>
    <servlet-class>FULLY QUALIFIED NAME OF THE CLASS</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>TestHandler</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

Please note :::: TestHandler is the servlet name and this servlet is mapped to url in the JSP (view) (url pattern /*)

Similarily for another servlet I am adding the servlet class

 <servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>FULLY QUALIFIED NAME OF THE CLASS</servlet-class>
  </servlet>

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

This all lines of code should be written in web.xml

1 Comment

Thank you! I know what you mean. I have the <servlet-class> part. But actually, jsp is a servlet defined in Tomcat/conf/web.xml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.