File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes getServerName(), getRemoteHost Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "getServerName(), getRemoteHost" Watch "getServerName(), getRemoteHost" New topic

getServerName(), getRemoteHost

Vasim Patel
Ranch Hand

Joined: Apr 29, 2004
Posts: 87

What is the difference between HttpServletRequest.getServerName() and getRemoteHost()

The javadoc says:

-getServerName():Returns the host name of the server that received the request. For HTTP servlets, same as the value of the CGI variable SERVER_NAME.
-getRemoteHost():Returns the fully qualified name of the client that sent the request.

If I am sending the request from the same machine as the webserver is, shouldn't they be the same?
I get my machine name for getServerName(), while a totally different machine address from getRemoteHost().

Am i missing something here?
Afroz Ahmed
Ranch Hand

Joined: Jan 18, 2004
Posts: 64
Hello Patel,

If the client and server are on same machine then
getServerName() returns the name(system's ip address) of your system
getRemoteHost() returns the fully qualified name(network name) of your system..

Example can be like

The value of an idea lies in the usage of it.
I agree. Here's the link:
subject: getServerName(), getRemoteHost
It's not a secret anymore!