英文:
In the internals of an ContextListener how can I find out the port the Web App is running on
问题
在ContextListener
的内部,如何找出Web应用程序正在运行的端口?
我有一个使用JSP页面作为前端的Java Web应用程序项目。该项目通过实现ServletContextListener
来连接后端。此ContextListener
在其contextInitialized
方法中通过实例化访问类DBQuery
来访问数据库:
ServletContext ctx = contextEvent.getServletContext();
dbQuery = new DBQuery();
ctx.setAttribute("dbQuery", dbQuery);
然后,JSP页面通过以下方式引用此DBQuery
对象:
getServletContext().getAttribute("dbQuery");
并根据需要调用DBQuery
的方法。
现在的问题是:在DBQuery
类中,根据Web应用程序运行的主机和端口,我需要执行不同的操作。
我找到了一种在DBQuery
中确定主机名的方法:
import java.net.InetAddress;
String hostName = InetAddress.getLocalHost().getHostName();
奇怪的是,InetAddress
似乎没有获取端口号的方法。在DBQuery
类中,我如何找出Web应用程序正在运行的端口?
英文:
In the internals of an ContextListener how can I find out the port the Web App is running on
I have a Java Web app project with JSP pages as frontend. The project implements a ServletContextListener
to connect to the backend. This ContextListener accesses the database by instantiating an access class DBQuery
in its contextInitialized
method:
ServletContext ctx = contextEvent.getServletContext();
dbQuery = new DBQuery();
ctx.setAttribute("dbQuery", dbQuery);
The JSP pages then refer to this DBQuery
object via
getServletContext().getAttribute("dbQuery");
and call the methods of DBQuery
as they desire.
Now the problem: In the DBQuery
class I need to do different things depending on which host and on which port the web app runs.
I found a way to determine the host name in DBQuery:
import java.net.InetAddress;
String hostName = InetAddress.getLocalHost().getHostName();
Strangely InetAddress
does not seem to have a method to get the port number. How can I find out in the DBQuery
class on which the port the web app runs on?
答案1
得分: 0
以下是翻译好的内容:
在查看了Steve的评论,参考了GitHub gist后,我编写了下面调整过的代码,完全实现了所要求的功能:
String port = "80"; try { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); Set<ObjectName> objs = mbs.queryNames( new ObjectName( "*:type=Connector,*" ), Query.match( Query.attr( "protocol" ), Query.value( "HTTP/1.1" ) ) ); for ( ObjectName obj : objs ) { String scheme = mbs.getAttribute( obj, "scheme" ).toString(); port = obj.getKeyProperty( "port" ); break; } } catch ( MalformedObjectNameException | UnknownHostException | MBeanException | AttributeNotFoundException | InstanceNotFoundException | ReflectionException ex ) { ex.printStackTrace(); }
所以主要的是实例化一个
MBeanServer
并利用其属性。
英文:
Following Steve's comment to look at a GitHub gist, I came up with the following adapted code which does exactly what was asked for:
String port = "80";
try {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> objs = mbs.queryNames( new ObjectName( "*:type=Connector,*" ),
Query.match( Query.attr( "protocol" ), Query.value( "HTTP/1.1" ) ) );
for ( ObjectName obj : objs ) {
String scheme = mbs.getAttribute( obj, "scheme" ).toString();
port = obj.getKeyProperty( "port" );
break;
}
} catch ( MalformedObjectNameException | UnknownHostException | MBeanException | AttributeNotFoundException | InstanceNotFoundException | ReflectionException ex ) {
ex.printStackTrace();
}
So the main thing is to instantiate a MBeanServer
and utilise its attributes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论