发布网友 发布时间:2022-04-23 22:54
共2个回答
热心网友 时间:2023-10-12 19:36
参考代码如下:
[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 "";
}
热心网友 时间:2023-10-12 19:36
servlet的request好像有getLoaclPort 一类的方法