Exact Path Mapping
An exact match occurs only when the request URL is identical to the string defined in the <url-pattern> tag. This is the most specific form of routing.
<servlet-mapping>
<servlet-name>resourceHandler</servlet-name>
<url-pattern>/secure/login</url-pattern>
</servlet-mapping>
http://localhost:8080/app/secure/login→ Matcheshttp://localhost:8080/app/secure/login/→ Does not matchhttp://localhost:8080/app/public/login→ Does not match
Extension Mapping
This method utilizes the asterisk wildcard * to match file extensions. The match is applied regardless of the directory structure, provided the file extension matches. A forward slash cannot be used in this pattern; otherwise, the container will fail to start.
<servlet-mapping>
<servlet-name>actionProcessor</servlet-name>
<url-pattern>*.svc</url-pattern>
</servlet-mapping>
http://localhost:8080/app/api/getUser.svc→ Matcheshttp://localhost:8080/app/deep/nested/save.svc→ Matcheshttp://localhost:8080/app/api/getUser→ Does not match
Path Mapping
Path matching routes requests based on a specific directory prefix. The /* suffix indicates that any resource residing within that path or its subdirectories should be processed.
<servlet-mapping>
<servlet-name>apiGateway</servlet-name>
<url-pattern>/api/v1/*</url-pattern>
</servlet-mapping>
http://localhost:8080/app/api/v1/data→ Matcheshttp://localhost:8080/app/api/v1/users/profile→ Matcheshttp://localhost:8080/app/v2/data→ Does not match
Default Servlet Mapping
Mapping to / designates the Servlet as the default handler for the application. It intercepts all requests except those specifically handled by the container for JSP files.
<servlet-mapping>
<servlet-name>defaultRouter</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
http://localhost:8080/app/home.html→ Matcheshttp://localhost:8080/app/style/main.css→ Matcheshttp://localhost:8080/app/list.jsp→ Does not match
Catch-All Mapping
Using /* forces the Servlet to handle every single request, including static assets and other content, overriding default container behavior.
<servlet-mapping>
<servlet-name>globalFilter</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
http://localhost:8080/app/anything→ Matches
Matching Precedence
When multiple patterns satisfy a single request URL, the container determines the target Servlet based on a strict hierarchy:
- Exact Match
- Longest Path Match
- Extension Match
Resolution Analysis
Consider the following mapping configuration:
PathAmapped to/report/*Rootmapped to/*ExactReportmapped to/reportExtHandlermapped to*.req
- Request:
/report/summary.html
Matches:/report/*and/*.
Result:PathAis invoked (Longest Path). - Request:
/report
Matches:/report/*and/report.
Result:ExactReportis invoked (Exact Match). - Request:
/report/data.req
Matches:/report/*and*.req.
Result:PathAis invoked (Path Match takes precedence over Extension). - Request:
/submit.req
Matches:/*and*.req.
Result:Rootis invoked (Path Match takes precedence over Extension).
Multiple Mapping Configurations
The web.xml descriptor allows associating several distinct URL patterns with a single Servlet. A specific URL cannot be mapped to two different Servlets simultaneously.
Approach 1: Single Mapping Block
All patterns are declared within a single <servlet-mapping> element.
<servlet-mapping>
<servlet-name>multiViewHandler</servlet-name>
<url-pattern>/secure/*</url-pattern>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
Approach 2: Multiple Mapping Blocks
Multiple <servlet-mapping> elements reference the same Servlet name.
<servlet-mapping>
<servlet-name>multiViewHandler</servlet-name>
<url-pattern>/secure/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>multiViewHandler</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>