简体   繁体   中英

Find the port of the server the application is running in java

嗨,我有一个运行在Tomcat中的应用程序以及Weblogic和Websphere,我需要确定该应用程序正在运行哪个端口并显示在UI上,是否有任何JAVA api可以找到该应用程序

如果您的应用程序已经通过该端口访问,则可以轻松地从HttpServletRequest获取它。

As mentioned by inkredibl the ServletRequest method getServerPort provides this information:

Returns the port number to which the request was sent. It is the value of the part after ":" in the Host header value, if any, or the server port where the client connection was accepted on.

You can't ask a server from the outside for which ports are used by which application. (Ahh, you cam try, but it shouldn't tell). That's obviously a security feature (you could start a port scan, but I guess, that's not exactly what you want...or?)

So the only way out - the application needs to communicate its port number to outer space, either by a simple web page, where the actual port number is desplayed (human-machine-interface) or a separate webservice with a static port number, where a client can request the actual server properties (machine-machine-interface).

(Note: I assumed, the 'UI' is a client application...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM