How Are Servlet Url Mappings in Web.Xml Used

How are Servlet url mappings in web.xml used?

From Servlet 3.0 specification, this is how the web container must locate the servlet after receiving a request (emphasis mine):

The path used for mapping to a servlet is the request URL from the
request object minus the context path and the path parameters. The
URL path mapping rules below are used in order. The first successful
match is used with no further matches attempted
:

  1. The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the
    servlet.
  2. The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory
    at a time, using the ’/’ character as a path separator. The longest
    match determines the servlet selected.
  3. If the last segment in the URL path contains an extension (e.g. .jsp), the servlet container will try to match a servlet that handles
    requests for the extension. An extension is defined as the part of
    the last segment after the last ’.’ character.
  4. If neither of the previous three rules result in a servlet match, the container will attempt to serve content appropriate for the
    resource requested. If a "default" servlet is defined for the
    application, it will be used. Many containers provide an implicit
    default servlet for serving content.

The container must use case-sensitive string comparisons for matching.

You should also look the specification of mappings (given below):

In the Web application deployment descriptor, the following syntax is
used to define mappings:

  • A string beginning with a ‘/’ character and ending with a ‘/*’ suffix
    is used for path mapping.

  • A string beginning with a ‘*.’ prefix is used as an extension mapping.

  • The empty string ("") is a special URL pattern that exactly maps to
    the application's context root, i.e., requests of the form
    http://host:port/<contextroot>/. In this case the path info is ’/’ and
    the servlet path and context path is empty string (““).

  • A string containing only the ’/’ character indicates the "default"
    servlet of the application. In this case the servlet path is the
    request URI minus the context path and the path info is null.

  • All other strings are used for exact matches only

Let us look at examples now. Consider the following set of mappings:


Path Pattern Servlet
/foo/bar/* servlet1
/baz/* servlet2
/catalog servlet3
*.bop servlet4

The following behavior would result:


Incoming Path Servlet Handling Request
/foo/bar/index.html servlet1
/foo/bar/index.bop servlet1
/baz servlet2
/baz/index.html servlet2
/catalog servlet3
/catalog/index.html “default” servlet
/catalog/racecar.bop servlet4
/index.bop servlet4

Note that in the case of /catalog/index.html and /catalog/racecar.bop, the
servlet mapped to “/catalog” is not used because the match is not exact.

Now coming to your problem :)

/path/test belongs to the 5th point of specification of mappings. Which means only the paths ending in /path/test will target servlet1.

However /path/test/* qualifies for the first point of the same specification. This means that:

.../path/test will be handled by servlet1 and

.../path/test/abc will be handled by servlet2

Which was verified by me in a test application.

URL Pattern for servlet mapping in web.xml

Map a servlet to the containing directory. Inside that servlet, take apart the URL path and forward to the appropriate named servlet.

Servlet Mapping using web.xml

It allows servlets to have multiple servlet mappings:

<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-path>foo.Servlet</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/enroll</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/pay</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/bill</url-pattern>
</servlet-mapping>

It allows filters to be mapped on the particular servlet:

<filter-mapping>
<filter-name>Filter1</filter-name>
<servlet-name>Servlet1</servlet-name>
</filter-mapping>

Your proposal would support neither of them. Note that the web.xml is read and parsed only once during application's startup, not on every HTTP request as you seem to think.

Since Servlet 3.0, there's the @WebServlet annotation which minimizes this boilerplate:

@WebServlet("/enroll")
public class Servlet1 extends HttpServlet {

See also:

  • How do servlets work? Instantiation, sessions, shared variables and multithreading
  • Difference between each instance of servlet and each thread of servlet in servlets?
  • Our Servlets wiki page

web.xml : URL mapping

The URL pattern of / has a special meaning. It namely indicates the "Default Servlet" URL pattern. So every request which does not match any of the other more specific URL patterns in web.xml will ultimately end up in this servlet. Note that this thus also covers static files like plain vanilla HTML/CSS/JS and image files! Normally, the "Default Servlet" is already provided by the servlet container itself (see e.g. Tomcat's DefaultServlet documentation). Overriding the "Default Servlet" in your own webapp should be done with extreme care and definitely not this way.

You need to give your index servlet a different URL pattern. It should be the same as the one you definied in <welcome-file>.

So in case of

<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>

you need to map the index servlet as follows

<servlet-mapping>
<servlet-name>Index</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>

Using an URL rewrite filter as suggested by other answer is unnecessary for the particular purpose you had in mind.

Difference between / and /* in servlet mapping url pattern

<url-pattern>/*</url-pattern>

The /* on a servlet overrides all other servlets, including all servlets provided by the servletcontainer such as the default servlet and the JSP servlet. Whatever request you fire, it will end up in that servlet. This is thus a bad URL pattern for servlets. Usually, you'd like to use /* on a Filter only. It is able to let the request continue to any of the servlets listening on a more specific URL pattern by calling FilterChain#doFilter().

<url-pattern>/</url-pattern>

The / doesn't override any other servlet. It only replaces the servletcontainer's built in default servlet for all requests which doesn't match any other registered servlet. This is normally only invoked on static resources (CSS/JS/image/etc) and directory listings. The servletcontainer's built in default servlet is also capable of dealing with HTTP cache requests, media (audio/video) streaming and file download resumes. Usually, you don't want to override the default servlet as you would otherwise have to take care of all its tasks, which is not exactly trivial (JSF utility library OmniFaces has an open source example). This is thus also a bad URL pattern for servlets. As to why JSP pages doesn't hit this servlet, it's because the servletcontainer's built in JSP servlet will be invoked, which is already by default mapped on the more specific URL pattern *.jsp.

<url-pattern></url-pattern>

Then there's also the empty string URL pattern . This will be invoked when the context root is requested. This is different from the <welcome-file> approach that it isn't invoked when any subfolder is requested. This is most likely the URL pattern you're actually looking for in case you want a "home page servlet". I only have to admit that I'd intuitively expect the empty string URL pattern and the slash URL pattern / be defined exactly the other way round, so I can understand that a lot of starters got confused on this. But it is what it is.

Front Controller

In case you actually intend to have a front controller servlet, then you'd best map it on a more specific URL pattern like *.html, *.do, /pages/*, /app/*, etc. You can hide away the front controller URL pattern and cover static resources on a common URL pattern like /resources/*, /static/*, etc with help of a servlet filter. See also How to prevent static resources from being handled by front controller servlet which is mapped on /*. Noted should be that Spring MVC has a built in static resource servlet, so that's why you could map its front controller on / if you configure a common URL pattern for static resources in Spring. See also How to handle static content in Spring MVC?



Related Topics



Leave a reply



Submit