Can anyone explain servlet mapping? Can anyone explain servlet mapping? java java

Can anyone explain servlet mapping?


I think I may know what is going on.

In your working web.xml you have set your servlet to be the default servlet (/ by itself is the default servlet called if there are no other matches), it will answer any request that doesn't match another mapping.

In Failed 1 your /* mapping does appear to be a valid path mapping. With the /* mapping in web.xml it answers all requests except other path mappings. According to the specification extension mappings are implicit mappings that are overwritten by explicit mappings. That's why the extension mapping failed. Everything was explicitly mapped to app.

In Failed 2, App is responsible for everything, except content that matches the static content mapping. To show what is happening in the quick test I set up. Here is an example. /some-static-content-folder/ contains test.png

Trying to access test.png I tried:

/some-static-content-folder/test.png

and the file was not found. However trying

/some-static-content-folder/some-static-content-folder/test.png

it comes up. So it seems that the Tomcat default servlet (6.0.16 at least) drops the servlet mapping and will try to find the file by using the remaining path. According to this post Servlet for serving static content Jetty gives the behavior you and I were expecting.

Is there some reason you can't do something like map a root directory for your rest calls. Something like app mapped to /rest_root/* than you are responsible for anything that goes on in the rest_root folder, but anywhere else should be handled by Tomcat, unless you make another explicit mapping. I suggest setting your rest servlet to a path mapping, because it declares the intent better. Using / or /* don't seem appropriate, since you have to map out the exceptions. Using SO as an example, my rest mappings would be something like

/users/* for the user servlet

/posts/* for the posts servlet

Mapping order

  1. Explicit (Path mappings)
  2. Implicit (Extension mappings)
  3. Default (/)

Please correct anything that I got wrong.


For reference, the "failed attempt #2" is perfectly correct in version of Tomcat >= to 6.0.29.

It was the result of a Tomcat bug that get fixed in version 6.0.29:

https://issues.apache.org/bugzilla/show_bug.cgi?id=50026

<!-- Correct for Tomcat >= 6.0.29 or other Servlet containers --><servlet-mapping>  <servlet-name>app</servlet-name>  <url-pattern>/</url-pattern></servlet-mapping><servlet-mapping>  <servlet-name>default</servlet-name>  <url-pattern>/some-static-content-folder/*</url-pattern></servlet-mapping>


I've never tried to map a servlet like this, but I would argue that /* does technically both start with / and end with /*, even though the same character is used for both matches.