2

I am developing a common artifact that can be used in multiple web applications. The developers will just have to add my listener class to their web.xml. In my listener, I have a need to get the web application name that my code is running under.

Although getServletContextName() will give a name, this name is the display-name coded in web.xml. I think display-name is optional so it is not reliable. So I would like to get the actual web application name. Is there a way to do this?

Thanks for looking.

4

2 回答 2

3

尝试使用此代码

String ApplicationName = ((HttpServletRequest) request).getContextPath()
                    .replace("/", "");
于 2015-07-13T10:51:36.597 回答
-1

我找到了一种方法来做到这一点......

new java.io.File(context.getRealPath("/")).getName()

上面给出了 Web 应用程序名称,即 WAR 名称。

Hardik,感谢您对此进行调查。

MaVRoSCy,显示名称可以是任何东西。它不必是 Web 应用程序名称。例如,所有 Maven Web 项目都将默认显示名称为“原型创建的 Web 应用程序”,我不想选择它。

于 2012-09-07T12:57:28.387 回答