Get the server port number from tomcat without a request

前端 未结 12 1553
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 06:32

Is there any Tomcat API or configuration available which can tell an application (probably on startup), what port its running on without a request?

Imagine a scenari

12条回答
  •  离开以前
    2020-11-28 07:17

    For anybody who is interested in how we solved this, here is the mock code

    Server server = ServerFactory.getServer();
            Service[] services = server.findServices();
            for (Service service : services) {
                for (Connector connector : service.findConnectors()) {
                    ProtocolHandler protocolHandler = connector.getProtocolHandler();
                    if (protocolHandler instanceof Http11Protocol
                        || protocolHandler instanceof Http11AprProtocol
                        || protocolHandler instanceof Http11NioProtocol) {
                        serverPort = connector.getPort();
                        System.out.println("HTTP Port: " + connector.getPort());
                    }
                }
    
    
            }
    

提交回复
热议问题