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.
Getting 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());
Restricting Access to the United States and Canada
Due to the United States government’s policy restricting the export of strong encryption outside the United States and Canada, some web sites must be careful about who they let download certain software. Servlets, with their ability to find out about the client machine, are well suited to enforce this restriction. These servlets can check the client machine and provide links for download only if ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access