Understanding Servlet URL Pattern Matching Mechanisms

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 → Matches
  • http://localhost:8080/app/secure/login/ → Does not match
  • http://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 → Matches
  • http://localhost:8080/app/deep/nested/save.svc → Matches
  • http://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 → Matches
  • http://localhost:8080/app/api/v1/users/profile → Matches
  • http://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 → Matches
  • http://localhost:8080/app/style/main.css → Matches
  • http://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:

  1. Exact Match
  2. Longest Path Match
  3. Extension Match

Resolution Analysis

Consider the following mapping configuration:

  • PathA mapped to /report/*
  • Root mapped to /*
  • ExactReport mapped to /report
  • ExtHandler mapped to *.req
  • Request: /report/summary.html
    Matches: /report/* and /*.
    Result: PathA is invoked (Longest Path).
  • Request: /report
    Matches: /report/* and /report.
    Result: ExactReport is invoked (Exact Match).
  • Request: /report/data.req
    Matches: /report/* and *.req.
    Result: PathA is invoked (Path Match takes precedence over Extension).
  • Request: /submit.req
    Matches: /* and *.req.
    Result: Root is 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>

Tags: java servlet web development XML Configuration

Posted on Fri, 15 May 2026 01:38:26 +0000 by CybJunior