1

We usually end up with writing <url-pattern>/*</url-pattern> in web.xml for any Filter in servlets.

<filter-mapping>
    <filter-name>requestRedirectorFilter</filter-name>
    <url-pattern>/action</url-pattern>
</filter-mapping>`.  

Now my doubt is how java identifies which is next servlet/jsp is? Because any request we make through

request.getRequestDispatcher("/ABCXYZ").forward(request, (HttpServletResponse)servletResponse);

to navigate on next servlet/jsp, container by default is going to search in web.xml. And in web.xml <url-pattern>/*</url-pattern> is already there for the filter we use. Exactly here actual problem begins.

If <url-pattern>/*</url-pattern> [which is acting like a universal receiver for any request] is already there in web.xml then How the heck container knows to follow <url-pattern>/ABCXYZ</url-pattern> instead <url-pattern>/*</url-pattern> ? Please share your views and knowledge on this front.

Braiam
  • 1
  • 11
  • 47
  • 78
Jain
  • 65
  • 2
  • 8

1 Answers1

2

Servlet Matching Procedure

A request may match more than one servlet-mapping in a given context. The servlet container uses a straightforward matching procedure to determine the best match.

The matching procedure has four simple rules.

  • First, the container prefers an exact path match over a wildcard path match.

  • Second, the container prefers to match the longest pattern.

  • Third, the container prefers path matches over filetype matches.

  • Finally, the pattern <url-pattern>/</url-pattern> always matches any request that no other pattern matches.


For example, a context web.xml file can map the home page for an online catalog to one pattern and the search page for the catalog to a different pattern, as shown below:

<servlet-mapping>
  <servlet-name>catalogBrowse</servlet-name>
  <url-pattern>/Catalog/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
  <servlet-name>catalogSearch</servlet-name>
  <url-pattern>/Catalog/search/*</url-pattern>
</servlet-mapping>

Below figure illustrates the matching process for a context. Since the container prefers to match the longest pattern, a URL that includes /Catalog/search/ always matches the mapping for catalogSearch rather than the mapping for catalogBrowse.

URL pattern matching

enter image description here


It is copied form the below link if your are not interested to go to the link.

Please have a look at URL Patterns where it is described in detail with examples.

Braj
  • 46,415
  • 5
  • 60
  • 76