getRemoteHost() Method Example

This method is defined in ServletRequest interface from javax.servlet package inherited by HttpServletRequest.

With this method, the servlet programmer can know the name of the client from which the request came.

Let us see what Java API says about this method.

  • java.lang.String getRemoteHost():
    Returns the fully qualified name of the client or the last proxy that sent the request. If the engine cannot or chooses not to resolve the hostname (to improve performance), this method returns the dotted-string form of the IP address. For HTTP servlets, same as the value of the CGI variable REMOTE_HOST.

The method getRemoteHost() returns the name of the client system as a string.

Let us see the output by writing a program.

HTML Program: ClientData.html

Enter Your Name

web.xml entry for ClientInformation servlet



Servelet Program:

import javax.servlet.*; 
import javax.servlet.http.*;

public class ClientInformation extends HttpServlet
  public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    PrintWriter out = res.getWriter();

    String str = req.getRemoteHost();
    out.println("req.getRemoteHost() :  " + str);


Output screen of ClientData.html with text field filled up. Ofcourse, this does not have any relevance with the method getRemoreAddr() method.


The output screen when submit button is clicked.


Observe, the name is localhost, used in HTML file in ACTION attribute.

To know the IP address of the client (not name), a separate program exists: request.getRemoteAddr() Method Example.

View all Servlets

Leave a Comment

Your email address will not be published.