简体   繁体   中英

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?

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.

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".

in servlet

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

you can use this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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