在ContextListener的内部,我如何找出Web应用程序正在运行的端口。

huangapple go评论66阅读模式
英文:

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 = &quot;80&quot;;
try {
     MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
     Set&lt;ObjectName&gt; objs = mbs.queryNames( new ObjectName( &quot;*:type=Connector,*&quot; ),
             Query.match( Query.attr( &quot;protocol&quot; ), Query.value( &quot;HTTP/1.1&quot; ) ) );
     for ( ObjectName obj : objs ) {
         String scheme = mbs.getAttribute( obj, &quot;scheme&quot; ).toString();
         port = obj.getKeyProperty( &quot;port&quot; );
         break;
     }
} catch ( MalformedObjectNameException | UnknownHostException | MBeanException | AttributeNotFoundException | InstanceNotFoundException | ReflectionException ex ) {
     ex.printStackTrace();
}

So the main thing is to instantiate a MBeanServer and utilise its attributes.

huangapple
  • 本文由 发表于 2020年10月20日 14:39:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64439742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定