简体   繁体   English

如何获取WAR文件的名称?

[英]How do I get the name of the WAR file?

How can a class get the name of the WAR file that is using it? 类如何获取正在使用它的WAR文件的名称?

This is for diagnostic purposes. 这是出于诊断目的。

ServletContext.getContextPath()

This returns the context path of the application (or "" for the root context). 这将返回应用程序的上下文路径(或“”用于根上下文)。 Within a servlet container, no two applications will ever have the same value for this. 在servlet容器中,没有两个应用程序具有相同的值。

EDIT: 编辑:

And for those who don't know what the context path is: it's the URI prefix for the application. 对于那些不知道上下文路径是什么的人:它是应用程序的URI前缀。 In most cases, it defaults to the name of the war file, unless you configure it explicitly. 在大多数情况下,它默认为war文件的名称,除非您明确配置它。 So if you have foo.war, then you'll access it at http://localhost:8080/foo/ , and the above function will return "/foo". 所以如果你有foo.war,那么你将在http://localhost:8080/foo/访问它,上面的函数将返回“/ foo”。

in servlet 在servlet中

String warName = new File(getServletContext().getRealPath("/")).getName();

you can use this. 你可以用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM