import javax.servlet.http.HttpServletRequest; public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { String ipAddress = request.getRemoteAddr(); System.out.println("Client IP Address: " + ipAddress); } }
import javax.servlet.http.HttpServletRequest; public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { String ipAddress = request.getRemoteAddr(); String requestUrl = request.getRequestURL().toString(); System.out.println("Client IP Address: " + ipAddress); System.out.println("Requested URL: " + requestUrl); // log the client request to a file or database } }In this example, the getRemoteAddr method is used to retrieve the client's IP address, while the getRequestURL method is used to retrieve the URL that the client requested. These values are then logged to a file or database for future analysis. The HttpServletRequest class is part of the javax.servlet.http package, which is included in the Java Servlet API.