Ebooks

Java HttpServletMapping

Java HttpServletMapping shows how to use HttpServletMapping, which was introduced in Servlet 4.0.

HttpServletMapping

HttpServletMapping is the new Servlet 4.0 API which can be used for the runtime discovery of URL mappings.

The servlet mapping is obtained from an HttpServletRequest instance, which has four methods:

Java HttpServletMapping example

In the following example, we use HttpServletMapping to find out information about URL mappings. The example is run on Tomcat. Note that we have to choose a recent Tomcat version which has JARs with Servlet 4.0 API.

$ tree
.
├── nb-configuration.xml
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── zetcode
    │   │           └── MyServlet.java
    │   └── webapp
    │       ├── index.html
    │       ├── META-INF
    │       │   └── context.xml
    │       └── WEB-INF
    └── test
        └── java

This is the project structure.

MyServlet.java
package com.zetcode;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletMapping;
import javax.servlet.http.HttpServletRequest;
import javax.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);
        
        ServletOutputStream out = response.getOutputStream();
        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.

ServletOutputStream out = response.getOutputStream();
out.println(builder.toString());

We send the string to the client.

index.html
<!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.

HttpServletMapping details
Figure: HttpServletMapping details

In this tutorial, we have shown how to use the new HttpServletMapping API introduced in Servlet 4.0. You might also be interested in the related tutorials: Java FileInputStream tutorial, Serving plain text from Java servlet, Java Servlets tutorial, Java ServletConfig tutorial, and Java tutorial.