Get application server name or ip and port in Java

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

We have two instances of the application server running on the same physical field, and therefore they have two active ports in the field, that is, 9080, 9081. What I would like to have is displayed <Application Server Name>:<Application Server Port> .

Any ideas? I am a full Java noob, sorry if this is the main question.

+6
java web-applications websphere
source share
3 answers

You can use the methods ServletRequest#getLocalXXX() .

+12
source share

The server hostname is part of the request, since it depends on what URL the client used for your host. The value you get in this way is determined on the client and should not be what you expect.

If you are interested in the local host name, you can try:

 String hostname = InetAddress.getLocalHost().getHostName(); 
+13
source share

Crunchify provides a good example for this.

 import java.net.InetAddress; import java.net.UnknownHostException; public class CrunchifyGetIPHostname { public static void main(String[] args) { InetAddress ip; String hostname; try { ip = InetAddress.getLocalHost(); hostname = ip.getHostName(); System.out.println("Your current IP address : " + ip); System.out.println("Your current Hostname : " + hostname); } catch (UnknownHostException e) { e.printStackTrace(); } } } 
0
source share

All Articles