tomcat 中程序获取端口

网上摘抄:

 

参考代码如下:
[java] view plaincopyprint?
/** 
* 根据协议和scheme获取服务端口号 
* @return 端口号 
*/ 
private static String getHttpPort(String protocol, String scheme) 

MBeanServer mBeanServer = null; 
if (MBeanServerFactory.findMBeanServer(null).size() > 0) 

mBeanServer = (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0); 


Set names = null; 
try 

names = mBeanServer.queryNames(new ObjectName("Catalina:type=Connector,*"), null); 

catch (Exception e) 

return ""; 


Iterator it = names.iterator(); 
ObjectName oname = null; 
while (it.hasNext()) 

oname = (ObjectName)it.next(); 
String pvalue = (String)mBeanServer.getAttribute(oname, "protocol"); 
String svalue = (String)mBeanServer.getAttribute(oname, "scheme"); 
if (protocol.equals(pvalue) && scheme.equals(svalue)) 

return ((Integer)mBeanServer.getAttribute(oname, "port")).toString(); 




return ""; 
}

posted on 2015-11-05 14:37  明明很久  阅读(1348)  评论(0)    收藏  举报