ZetCode

Java HttpServletMapping

最后修改于 2024 年 1 月 27 日

在本文中,我们将展示如何使用 HttpServletMapping 在运行时发现 URL 映射。

HttpServletMapping

HttpServletMapping 是新的 Servlet 4.0 API,可用于在运行时发现 URL 映射。

servlet 映射是从一个 HttpServletRequest 实例中获取的,该实例有四个方法:

Java HttpServletMapping 示例

在下面的示例中,我们使用 HttpServletMapping 来查找有关 URL 映射的信息。该示例在 Tomcat 上运行。请注意,我们必须选择一个包含 Servlet 4.0 API JAR 包的较新 Tomcat 版本。

├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── zetcode
    │   │           └── MyServlet.java
    │   ├── resources
    │   └── webapp
    │       ├── index.html
    │       └── WEB-INF
    └── test
        └── java

这是项目结构。

pom.xml
<?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.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>6.0.0</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.3</version>
            </plugin>

            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>11.0.11</version>
                <configuration>
                    <webApp>
                        <contextPath>/app</contextPath>
                    </webApp>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

这是 pom.xml 文件。

com/zetcode/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());
    }
}

我们获取映射信息,并将其作为文本数据发送给客户端。

@WebServlet(name = "MyServlet", urlPatterns = {"/getMessage"})

我们使用 @WebServlet 以声明方式设置 servlet 绑定的 URL 模式。

HttpServletMapping mapping = request.getHttpServletMapping();
String mapName = mapping.getMappingMatch().name();
String value = mapping.getMatchValue();
String pattern = mapping.getPattern();
String servletName = mapping.getServletName();

我们从 request 对象中通过 getHttpServletMapping 获取 servlet 映射。我们调用了所有四个方法。

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());

我们将该字符串发送给客户端。

webapp/index.html
<!DOCTYPE html>
<html>
    <head>
        <title>Home Page</title>
        <meta charset="UTF-8">
    </head>
    <body>
        <a href="getMessage">Get message</a>
    </body>
</html>

这是一个主页。它包含一个调用 servlet 的链接。

$ curl localhost:8080/app/getMessage
Mapping type: EXACT; Match value: getMessage; Pattern: /getMessage; Servlet name: MyServlet

我们使用 curl 工具创建一个请求。

在本文中,我们展示了如何使用 HttpServletMapping

来源

Java HttpServletMapping - 文档

作者

我的名字是 Jan Bodnar,我是一名充满热情的程序员,拥有丰富的编程经验。我从 2007 年开始撰写编程文章。迄今为止,我已经撰写了超过 1400 篇文章和 8本电子书。我拥有超过十年的编程教学经验。

列出所有Java教程