views:

379

answers:

2

We would like to identify and display the server and port that a Java application is running on that is behind a proxy web server. This means that getServerName() and getServerPort() return the server name of the proxy and its port (80).

We have two application server instances running on a single physical box and therefore have two active ports per box i.e. 9080, 9081. What I'd like to have is <Application Server Name>:<Application Server Port> displayed.

Any ideas? I'm a complete Java noob, sorry if this is a basic question.

+1  A: 

You can use ServletRequest#getLocalXXX() methods for this.

BalusC
This and a combination of the rsp's answer below. getLocalName provides the IP address. InetAddress.getLocalHost.getHostName gives me the server name. getLocalPort provides the correct port number. Awesome, thanks!
Mike Daniels
You're welcome. However interesting that `getLocalName()` didn't give the hostname back.
BalusC
I wonder if the problem is because the application is behind IIS as a proxy and IIS directs to the application server via the IP address.
Mike Daniels
A: 

The server hostname is part of the request, as it depends on what URL the client used to reach your host. The value you get in this way is defined on the client and does not have to be what you expect.

If you are interested in the local hostname, you can try:

String hostname = InetAddress.getLocalHost().getHostName();
rsp