Java HttpServletMapping
last modified November 30, 2026
In this article we show how to use HttpServletMapping to discover URL mappings at runtime.
HttpServletMapping
HttpServletMapping is a Servlet 4.0 API for the runtime discovery of
URL mappings. The example uses Jakarta Servlet 6.1.
The servlet mapping is obtained from an HttpServletRequest instance,
which has four methods:
- getMappingMatch — returns the type of the match
- getPattern — returns the URL pattern that activated the servlet request
- getMatchValue — returns the String that was matched
- getServletName — returns the name of the servlet that was activated with the request
Java HttpServletMapping example
In the following example, we use HttpServletMapping to find out information
about URL mappings. The example is run on Jetty 12.1 with Jakarta Servlet 6.1.
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── zetcode
│ │ └── MyServlet.java
│ ├── resources
│ └── webapp
│ ├── index.html
│ └── WEB-INF
└── test
└── java
This is the project structure.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zetcode</groupId>
<artifactId>HttpServletMappingEx</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.release>25</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.5.1</version>
</plugin>
<plugin>
<groupId>org.eclipse.jetty.ee11</groupId>
<artifactId>jetty-ee11-maven-plugin</artifactId>
<version>12.1.15</version>
<configuration>
<webApp>
<contextPath>/app</contextPath>
</webApp>
</configuration>
</plugin>
</plugins>
</build>
</project>
This is the pom.xml file.
package com.zetcode;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletMapping;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet(name = "MyServlet", urlPatterns = {"/getMessage"})
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain;charset=UTF-8");
HttpServletMapping mapping = request.getHttpServletMapping();
String mapName = mapping.getMappingMatch().name();
String value = mapping.getMatchValue();
String pattern = mapping.getPattern();
String servletName = mapping.getServletName();
StringBuilder builder = new StringBuilder();
builder.append("Mapping type: ").append(mapName)
.append("; Match value: ").append(value)
.append("; Pattern: ").append(pattern)
.append("; Servlet name: ").append(servletName);
PrintWriter out = response.getWriter();
out.println(builder.toString());
}
}
We get the mapping info and send it to the client as text data.
@WebServlet(name = "MyServlet", urlPatterns = {"/getMessage"})
We set the URL patter to which the servlet is bound declaratively with @WebServlet.
HttpServletMapping mapping = request.getHttpServletMapping(); String mapName = mapping.getMappingMatch().name(); String value = mapping.getMatchValue(); String pattern = mapping.getPattern(); String servletName = mapping.getServletName();
From the request object, we get the servlet mapping with getHttpServletMapping.
We call all four methods.
StringBuilder builder = new StringBuilder();
builder.append("Mapping type: ").append(mapName)
.append("; Match value: ").append(value)
.append("; Pattern: ").append(pattern)
.append("; Servlet name: ").append(servletName);
From the data we build one string.
PrintWriter out = response.getWriter(); out.println(builder.toString());
We get a character writer and send the string to the client.
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
<meta charset="UTF-8">
</head>
<body>
<a href="getMessage">Get message</a>
</body>
</html>
This is a home page. It has a link that calls the servlet.
$ curl localhost:8080/app/getMessage Mapping type: EXACT; Match value: getMessage; Pattern: /getMessage; Servlet name: MyServlet
We create a request with the curl tool.
In this article we have shown how to use the HttpServletMapping.
Source
Java HttpServletMapping - documentation
Author
List all Java tutorials.