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(Oracle9i AS
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 Oracle9i AS Container
for J2EE(OC4J) 9.0.3 or later. ( Download from here
)
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.
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.
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
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.
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/*
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
Start the OC4J server.
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>
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
Bind the web application to a context root
using the following command:
Here, localhost is
your machine where OC4J instance is running. 8888 is the default port
of OC4J.
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.
From JDeveloper, open the ServletClientInfo.jws workspace file. This
will open the corresponding project and source files of the application.
Compile the ServletClientInfo.jpr
project.
Click on the GetServletClientInfo.java and run the file.
This will invoke the servlet which displays
the IP Address and the hostname of the client.
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".