How to get the name of the WAR file?

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

This is for diagnostic purposes.

+4
source share
3 answers

to servlet

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

you can use this.

+4
source

ServletContext.getContextPath()

This returns the application context path (or "" for the root context). Inside the servlet container, none of the two applications will ever have the same value for this.

EDIT:

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

+5
source

Another way to get the path works for anything, and not just for war and servlet, great for debugging:

  URL resource = this.getClass().getClassLoader().getResource(""); System.out.println("resource = " + resource); 
+1
source

Source: https://habr.com/ru/post/1401689/


All Articles