How to read the client Information of a Servlet Request

How To Read/Access The Client Information of a Servlet Request

Date: 20/03/2004

This document will help you understand:  
• How to Read/Access the Client Information of a Servlet Request
• How to run the sample Servlet application 

Table of Contents

Introduction
Prerequisites
Software Requirements
How to Read/Access Client Information of a Servlet Request
Running the Sample Servlet Applications
Useful References
Troubleshooting

Introduction

Servlets provide a component-based platform-independent method for building web-based applications, and are generally used to generate dynamic web content. To put it simply, a servlet receives a request from a client (the web browser, for instance), processes the request, and returns the response to the client. For example, the response can be some content queried from a database that is displayed using HTML.

For more information about Servlets, refer to the Useful References section later in this document.

What is Client Information of a Servlet Request?

A client in the web application paradigm is a program that runs on some kind of a device and accesses a web application. A good example of a client is a web browser on any system. The web browser communicates with many servers across the world to download and display data.

A web application is built with different components placed in different tiers. A simple example of a 2-tier application would be a client-server application where several clients (or end-user machines) access one central database server. Similarly, a 3-tier application consists of a database at the end tier, an application sever in the middle tier and clients that access the application at the client tier. For example, we can have Oracle database at the backend, an application server like OC4J(Oracle9AS Container for J2EE) or Oracle9i AS in the middle, and a variety of clients accessing the information from the web like a simple web browser, a wireless device, Palm Tops etc in the client tier. The application server hosts the applications that will be accessed by different clients.

Any servlet application can be accessed from a web browser. The system on which the browser is installed is the client, that has requested the servlet application. The request that comes from a web browser is transferred using HttpServletRequest object over HTTP. The Web Server receives the request and processes it. It is possible to know the client that requested the servlet application. The client's remote IP address and the hostname can be obtained from the HttpServletRequest object using HttpServletRequest.getRemoteAddr() and HttpRequest.getHostname() methods respectively. Alternatively, the client information can also be obtained from the InetAddress object that has implemented the earlier mentioned methods.

Prerequisites

To work your way through this HowTo, it is necessary to have a basic understanding of the fundamental concepts of servlets, and how to develop and deploy them to a web server.

Software Requirements

  • Oracle10g JDeveloper or later ( JDeveloper is Oracle's Visual Java Development Tool and can be downloaded from here)

  • or Oracle9AS Container for J2EE(OC4J) 9.0.3 or later. ( Download from here
  • JDK1.3.x or later. ( Download from here )

How To Read/Access the Client Information of a Servlet Request

This section provides with the steps to read and access Client information within a servlet.

Reading the Client's information from the HttpRequest object

    The hostname and IP Address of the client requesting the servlet can be obtained using the HttpRequest object. Following is the API description and code snippet for retrieving the hostname and the IP Address.

    getRemoteAddr(): Returns the internet address of the client sending the request. When the client address is unknown, returns an empty string.

    getRemoteHost(): Returns the host name of the client sending the request. If the name is unknown, returns an empty string. The fully qualified domain name (e.g. "oracle.com") of the client that made the request. The IP address is returned if this cannot be determined.

      ......
      
      public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException  {
            
             // Get IP Address of the client
             String ipaddress = request.getRemoteAddr();
             
             // Get Hostname of the client
             String hostName  = request.getRemoteHost();
        
             System.out.println("Client IP Address is: "+ipaddress);
             System.out.println("Client hostname is: "+hostName);
         .....
      }                     
      ...... 

Reading the Client's information from the InetAddress object

    Alternatively, the IP Address and the hostname of the client that sent the servlet request can be obtained from the InetAddress object also. Following is the code snippet to read the IP Address and the hostname from the InetAddress object.

    import java.net.InetAddress;
    import java.net.UnknownHostException; 
                
      ......
      
      public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException  {
            
        String ipaddress = null;
        String hostName  = null;
            
        try { 
          // Get the InetAddress.
          InetAddress inet = InetAddress.getLocalHost(); 
              
          // Get IP Address of the client
          ipaddress = inet.getHostAddress();
               
          // Get hostname of the client
          hostName  = inet.getHostName(); 
              
          System.out.println("Client IP Address is: "+ipaddress);    
          System.out.println("Client hostname is: "+hostName);    
              
        } catch( UnknownHostException uhe ){
            System.out.println("UnknownHostException: "+uhe.toString());
        }                                                      
        .....
      }                     
      .......

Running the Sample Servlet Application

This section talks about how to get the servlet up and running.

Running the Servlet Sample Application in OC4J (EAR Deployment):

    The sample application ServletClientInfo.jar can be downloaded from here. The Jar file extracts all the sample application files to a folder called ServletClientInfo. Go through the following steps to deploy and run the servlet in OC4J. 

    1. Add servlet.jar to the CLASSPATH; this file is present under <OC4J_HOME>/j2ee/home/lib folder.
      Note: <OC4J_HOME> is the folder where the OC4J server is installed; for example, d:\oc4j
    2. From the ServletClientInfo folder, compile the servlet file: GetServletClientInfo.java using the following command:
      javac src/oracle/otnsamples/servlets/*.java -d ./WEB-INF/classes
      - This creates the class for the servlet file under the WEB-INF/classes/oracle/otnsamples/servlets folder.
    3. Next, create a WAR file containing all the servlet classes, JSPs, HTML files and the web.xml file using the JAR command:
      jar -cvfM ServletClientInfo.war WEB-INF/*

    4. Create a EAR file containg the application.xml file and WAR file using the JAR command again.
      jar -cvfM ServletClientInfo.ear ServletClientInfo.war META-INF
    5. Start the OC4J server.
    6. Deploy the servlet applications using the command below:

      java -jar <OC4J_HOME>/j2ee/home/admin.jar ormi://<server>:<rmiport> admin <admin_password> -deploy -file <Sample_Name>.ear -deploymentName <earDeploymentName>

      Example:
      java -jar D:/oc4j903/j2ee/home/admin.jar ormi://incq207a.idc.oracle.com:23791 admin welcome -deploy -file ServletClientInfo.ear -deploymentName ServletClientInfo

      Note:
      <server> is the IP Address of the system where the OC4J instance is running.
      <rmiport> is the RMI port number of the OC4J instance; default is 23791
      <admin_password> is the password of the OC4J installation
      <Sample_Name>.ear is the EAR file created, that is, ServletClientInfo.ear
      <earDeploymentName> is the deployment name for the application, that is, ServletClientInfo

    7. Bind the web application to a context root using the following command:

      java -jar <OC4J_HOME>/j2ee/home/admin.jar ormi://<server>:<rmiport> <admin_username> <admin_password> -bindWebApp <earDeploymentName> <Sample_War_Name> http-web-site /<website_root>

      Example:
      java -jar D:/oc4j903/j2ee/home/admin.jar ormi://incq207a.idc.oracle.com:23791 admin welcome -bindWebApp ServletClientInfo ServletClientInfo http-web-site /ServletClientInfo

    8. Access the servlets using the URLs like this:

      http://localhost:8888/ServletClientInfo/servlet/GetServletClientInfo
    9. Here, localhost is your machine where OC4J instance is running. 8888 is the default port of OC4J.

    10. This will invoke the servlet which displays the Server information like the name, its version and the port at which it is running.

Running the Servlet from Oracle JDeveloper9i:

    The sample application ServletClientInfo.jar can be downloaded from here. The jar file extracts all the sample application files to a folder called ServletClientInfo. Go through the following steps to run the servlet from JDeveloper.

    1. From JDeveloper, open the ServletClientInfo.jws workspace file. This will open the corresponding project and source files of the application.

    2. Compile the ServletClientInfo.jpr
    3. project.
    4. Click on the GetServletClientInfo.java and run the file. 
    5. This will invoke the servlet which displays the IP Address and the hostname of the client.

Useful References

Troubleshooting

Proxy server IP Address/hostname is displayed instead of Clients IPAddress/Hostname:
If the web browser settings has the proxy server configured, then the request will come through the proxy server and so, the servlet application displays the hostname and the IP address of the proxy-server instead of the actual local host. To know the actual client info, in the browser settings, map your proxy-connection to point to "Direct connection to the Internet".
 

Do enter your comments about this HowTo in the OTN Sample Code Discussion Forum.


How To Read Servlet Client Information

Please rate this how-to :
Excellent
Good
Average
Below Average
Poor




E-mail this page
Printer View Printer View