The IP address of client in servlet

The IP address of client in servlet

To get the IP address of a client in a servlet, you can use the HttpServletRequest interface. The HttpServletRequest provides methods to retrieve information about the request, including the IP address of the client that made the request. The primary method to obtain the client’s IP address is getRemoteAddr().

However, if the client is behind a proxy or a load balancer, the getRemoteAddr() method may return the IP address of the proxy or load balancer instead of the actual client. To handle such cases, you can check the X-Forwarded-For header, which may contain the client’s real IP address.

IP address of client

Key Points to Retrieve Client IP Address in a Servlet

1.  HttpServletRequest Method:

   getRemoteAddr(): Returns the IP address of the client or last proxy that sent the request.

2.  X-Forwarded-For Header:

   Check the X-Forwarded-For header, which is commonly used by proxies and load balancers to pass the original client IP address.

Java Example

Here is an example servlet that demonstrates how to get the IP address of the client

Example
Here is an example servlet that demonstrates how to get the IP address of the client:

java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/ClientIPServlet")
public class ClientIPServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        // Get client IP address from request
        String ipAddress = request.getHeader("X-Forwarded-For");
        if (ipAddress == null || ipAddress.isEmpty()) {
            ipAddress = request.getRemoteAddr();
        }

        out.println("<html><body>");
        out.println("<h1>Client IP Address</h1>");
        out.println("<p>IP Address: " + ipAddress + "</p>");
        out.println("</body></html>");

        out.close();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

Explanation The IP address of client in servlet

  • Retrieving IP Address: request.getHeader(“X-Forwarded-For”): Checks if the X-Forwarded-For header is present, which may contain the real IP address of the client if they are behind a proxy.
  • request.getRemoteAddr(): Retrieves the IP address of the client or the last proxy that sent the request.
  • Handling Null or Empty Header: If the X-Forwarded-For header is null or empty, fall back to getRemoteAddr() to get the client’s IP address.
  • Output the IP Address: The IP address is displayed on a simple HTML page.

Homepage

Readmore