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


  efgh
  ClientInformation



  efgh
  /jasmine

Servelet Program: ClientInformation.java

import javax.servlet.*; 
import javax.servlet.http.*;
import java.io.*; 

public class ClientInformation extends HttpServlet
{
  public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
  {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

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

    out.close();
  }
}

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

getRemoteHost()

The output screen when submit button is clicked.

ima

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.