1、方法

  • [x] //获取Servlet路径

    1. `String servletpath = request.getServletPath();`<br /> `System.out.println(servletpath);`

    2、实现

    ```java package servlet;

import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException; import java.io.PrintWriter; import java.net.HttpCookie; import java.net.SocketTimeoutException;

/**

  • @Author: 小雷学长
  • @Date: 2022/3/20 - 17:22
  • @Version: 1.8 */ public class RequestTestServlet extends HttpServlet {

    @Override protected void doGet(HttpServletRequest request, HttpServletResponse response)

    1. throws ServletException, IOException {
    2. //获取Servlet路径
    3. String servletpath = request.getServletPath();
    4. System.out.println("servletpath");
    5. response.setContentType("text/html");
    6. PrintWriter out = response.getWriter();
    7. out.print(servletpath);

    } } xml <?xml version=”1.0” encoding=”UTF-8”?>

    RequestTestServlet servlet.RequestTestServlet RequestTestServlet /testrequest

``` image.png