获取 web 服务器 port

Posted xiluhua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取 web 服务器 port相关的知识,希望对你有一定的参考价值。

Tomcat:

public static String getServerPort(boolean secure) throws AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException {  
        MBeanServer mBeanServer = null;  
        if (MBeanServerFactory.findMBeanServer(null).size() > 0) {  
            mBeanServer = (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);  
        }  
          
        if (mBeanServer == null) {  
            System.out.println("调用findMBeanServer查询到的结果为null");  
            return "";  
        }  
          
        Set<ObjectName> names = null;  
        try {  
            names = mBeanServer.queryNames(new ObjectName("Catalina:type=Connector,*"), null);  
        } catch (Exception e) {  
            return "";  
        }  
        Iterator<ObjectName> it = names.iterator();  
        ObjectName oname = null;  
        while (it.hasNext()) {  
            oname = (ObjectName)it.next();  
            String protocol = (String)mBeanServer.getAttribute(oname, "protocol");  
            String scheme = (String)mBeanServer.getAttribute(oname, "scheme");  
            Boolean secureValue = (Boolean)mBeanServer.getAttribute(oname, "secure");  
            Boolean SSLEnabled = (Boolean)mBeanServer.getAttribute(oname, "SSLEnabled");  
            if (SSLEnabled != null && SSLEnabled) {// tomcat6开始用SSLEnabled  
                secureValue = true;// SSLEnabled=true但secure未配置的情况  
                scheme = "https";  
            }  
            if (protocol != null && ("HTTP/1.1".equals(protocol) || protocol.contains("http"))) {  
                if (secure && "https".equals(scheme) && secureValue) {  
                    return ((Integer)mBeanServer.getAttribute(oname, "port")).toString();  
                } else if (!secure && !"https".equals(scheme) && !secureValue) {  
                    return ((Integer)mBeanServer.getAttribute(oname, "port")).toString();  
                }  
            }  
        }  
        return "";  
    }  

Weblogic参考:

http://blog.csdn.net/yunzhu666/article/details/8662039

 

以上是关于获取 web 服务器 port的主要内容,如果未能解决你的问题,请参考以下文章

Akka应用程序到Heroku获取错误:Web进程无法绑定到$ PORT

代码片段 - Golang 实现简单的 Web 服务器

markdown 在Intel Nuc上设置Ubuntu Web服务器,步骤和代码片段

七个办法只有一个有效:200 PORT command successful. Consider using PASV.425 Failed to establish connection.(代码片段

在 python 上获取“错误 R10(启动超时)-> Web 进程未能在启动后 60 秒内绑定到 $PORT”

Choose unique values for the 'webAppRootKey' context-param in your web.xml files! 错误的解决(代码片段