Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if I have two servlet mappings in web.xml that match a request?

What happens if I have two servlet mappings in web.xml that match a request? Does it choose most specific?

For example if I have the following xml and a request comes to ..../something while it go to somethingservlet or everything_else servlet?

   <servlet-mapping>
      <servlet-name>something</servlet-name>
      <url-pattern>/something</url-pattern>
  </servlet-mapping>    

  <servlet-mapping>
    <servlet-name>everything_else</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
like image 678
Usman Ismail Avatar asked Aug 08 '12 20:08

Usman Ismail


People also ask

Can we have multiple servlets in Web xml?

You can declare multiple servlets using the same class with different initialization parameters. The name for each servlet must be unique across the deployment descriptor. The <servlet-mapping> element specifies a URL pattern and the name of a declared servlet to use for requests whose URL matches the pattern.

Can a servlet mapping have more than one URL pattern?

Previous versions of the servlet schema allows only a single url-pattern in a filter mapping. For filters mapped to multiple URLs this results in needless repetition of whole mapping clauses.

Can we have multiple URL pattern in Web xml?

Yes that works just fine, but that is Servlet 2.4 style and I am trying to avoid the problem of extra typing. because <url-pattern> element is allowed only once under <filter-mapping> . "Multiple <url-pattern> elements should be fine, but the value /einwenig/*.

In which directory can we put Web xml in servlet?

The web. xml file is located in the WEB-INF directory of your Web application. The first entry, under the root servlet element in web.


1 Answers

First succesful match will be used.

There are certain mapping rules follwed by servlet container. Read Servlet 2.5 specification chapter SRV.11:

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.
like image 95
kosa Avatar answered Sep 21 '22 05:09

kosa