1. package servlet;
    2. import jakarta.servlet.ServletException;
    3. import jakarta.servlet.http.HttpServlet;
    4. import jakarta.servlet.http.HttpServletRequest;
    5. import jakarta.servlet.http.HttpServletResponse;
    6. import java.io.IOException;
    7. import java.io.PrintWriter;
    8. import java.net.HttpCookie;
    9. /**
    10. * @Author: 小雷学长
    11. * @Date: 2022/3/20 - 17:22
    12. * @Version: 1.8
    13. */
    14. public class RequestTestServlet extends HttpServlet {
    15. @Override
    16. protected void doGet(HttpServletRequest request, HttpServletResponse response)
    17. throws ServletException, IOException {
    18. response.setContentType("text/html");
    19. PrintWriter out = response.getWriter();
    20. out.print("");
    21. //获取客户端的IP地址
    22. String remoteAddr = request.getRemoteAddr();
    23. System.out.println("客户端的IP地址:" + remoteAddr);
    24. }
    25. }
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    5. version="4.0">
    6. <servlet>
    7. <servlet-name>RequestTestServlet</servlet-name>
    8. <servlet-class>servlet.RequestTestServlet</servlet-class>
    9. </servlet>
    10. <servlet-mapping>
    11. <servlet-name>RequestTestServlet</servlet-name>
    12. <url-pattern>/testrequest</url-pattern>
    13. </servlet-mapping>
    14. </web-app>

    image.png
    image.png