Java EE:如何获取我的应用程序的URL?
问题描述:
在Java EE中,如何动态检索应用程序的完整URL?
In Java EE, how can I dynamically retrieve the full URL for my application?
例如,如果URL是localhost:8080 / myapplication /
,我想要一个方法可以简单地将它作为字符串或其他内容返回给我。
For example, if the URL is "localhost:8080/myapplication/"
, I would like a method that can simply return this to me, either as a String or something else.
我正在运行GlassFish作为应用程序服务器。
I am running GlassFish as application server.
答
在任何servlet或过滤器中,您都可以访问 HttpServletRequest
,它允许您查看客户端使用的URL你的申请,例如尝试 HttpServletRequest。 getRequestURL()
。
Inside any servlet or filter you have access to HttpServletRequest
which allows you to see what URL was used by the client to access your application, e.g. try HttpServletRequest.getRequestURL()
.