首页 > 其他 > 详细

tomcat 中程序获取端口

时间:2015-11-05 15:21:06      阅读:159      评论:0      收藏:0      [点我收藏+]

网上摘抄:

 

参考代码如下:
[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 ""; 
}

tomcat 中程序获取端口

原文:http://www.cnblogs.com/recordmytime/p/4939467.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!