Java Servlets   «Prev 

The Client

For each request, a servlet has the ability to find out about the client machine and, for pages requiring authentication, about the actual user. This information can be used for logging access data, associating information with individual users, or restricting access to certain clients.

Retrieving Information About the Client Machine

A servlet can use getRemoteAddr() and getRemoteHost() to retrieve the IP address and hostname of the client machine, respectively:
public String ServletRequest.getRemoteAddr()
public String ServletRequest.getRemoteHost()

Both values are returned as String objects. The information comes from the socket that connects the server to the client, so the remote address and hostname may be that of a proxy server. An example remote address might be "192.26.80. 118" while an example remote host might be "dist.engr. sgi.com". The IP address or remote hostname can be converted to a java.net.InetAddress object using InetAddress.getByName():

InetAddress remoteInetAddress = InetAddress.getByName(req.getRemoteAddr());

Client and Server interaction with Java Servlets

The first image shows how the webserver handles servlet request.

A new process is spawned

A thread is started for this request

Another user is at this site at the same time

This user makes a request to run the same servlet.

No additional processes are started

Another thread is started for this request

First servlet thread returns HTML to the user

The thread ends, but the servlet is still running

Second servlet thread returns HTML to the user

The second thread ends, but the servlet is still running.