Java Servlets   «Prev 

Client Servlet Interaction

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

1) Webserver handles servlet request.
1) The first image shows how the webserver handles servlet request.

2) A new process is spawned
2) A new process is spawned

3) A thread is started for this request
3) A thread is started for this request

4) Another user is at this site at the same time
4) Another user is at this site at the same time

5) This user makes a request to run the same servlet.
5) This user makes a request to run the same servlet.

6) No additional processes are started
6) No additional processes are started

7) Another thread is started for this request
7) Another thread is started for this request

8) First servlet thread returns HTML to the user
8) First servlet thread returns HTML to the user

9) The thread ends, but the servlet is still running
9) The thread ends, but the servlet is still running

10) Second servlet thread returns HTML to the user
10) Second servlet thread returns HTML to the user

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

Java Servlets