How do I get the name of the WAR file?

折月煮酒 提交于 2020-01-03 09:05:10

问题


How can a class get the name of the WAR file that is using it?

This is for diagnostic purposes.


回答1:


in servlet

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

you can use this.




回答2:


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.

EDIT:

And for those who don't know what the context path is: it's the URI prefix for the application. In most cases, it defaults to the name of the war file, unless you configure it explicitly. So if you have foo.war, then you'll access it at http://localhost:8080/foo/, and the above function will return "/foo".



来源:https://stackoverflow.com/questions/9727507/how-do-i-get-the-name-of-the-war-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!