问题
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