在 Java 中获取应用程序服务器名称或 IP 和端口 [英] Get Application Server name or ip and port in Java
问题描述
我们想识别并显示在代理 Web 服务器后面运行 Java 应用程序的服务器和端口.这意味着 getServerName() 和 getServerPort() 返回代理的服务器名称及其端口 (80).
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).
我们有两个应用服务器实例运行在一个物理机器上,因此每个机器有两个活动端口,即 9080、9081.我想要的是
.
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.
有什么想法吗?我是一个完整的 Java 菜鸟,抱歉,如果这是一个基本问题.
Any ideas? I'm a complete Java noob, sorry if this is a basic question.
推荐答案
您可以为此使用 ServletRequest#getLocalXXX()
方法.
You can use ServletRequest#getLocalXXX()
methods for this.
ServletRequest#getLocalName()
返回本地主机名.ServletRequest#getLocalAddr()
返回本地 IP.ServletRequest#getLocalPort()
返回本地端口.
这篇关于在 Java 中获取应用程序服务器名称或 IP 和端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!