The Server
A servlet can find out much about the server in which it is executing. It can learn the hostname, listening port, and server software, among other things. A servlet can display this information to a client, use it to customize its behavior based on a particular server package, or even use it to explicitly restrict the machines on which the servlet will run.
Getting Information About the Server
There are four methods that a servlet can use to learn about its
server: two that are called using the
ServletRequest object passed to the servlet and
two that are called from the ServletContext object
in which the servlet is executing. A servlet can get the name of the
server and the
port number for a particular request
with getServerName() and
getServerPort(), respectively:
public String ServletRequest.getServerName() public int ServletRequest.getServerPort()
These methods are attributes of ServletRequest
because the values can change for different requests if the server
has more than one name (a technique called
virtual
hosting). The returned name might be something like
"www.servlets.com" while the returned port might
be something like "8080".
The
getServerInfo()
and
getAttribute()
methods of ServletContext provide information
about the server software and its attributes:
public String ServletContext.getServerInfo() public Object ServletContext.getAttribute(String name)
getServerInfo() returns the name and version of the server software, separated by a slash. The string returned ...
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