Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 803
finneycanhelp Avatar asked Mar 15 '12 20:03

finneycanhelp


People also ask

How do I name a WAR file?

To rename the WAR file: Use the rename command to rename the WAR file. Enter the command without the line break. Where custom_name is the context root or custom application name you choose.

Where can I find WAR file?

Static HTML files and JSP are stored at the top level of the WAR directory. The top-level directory contains the WEB-INF sub-directory which contains the following: Server-side classes (Servlets, JavaBean components and related Java class files) must be stored in the WEB-INF/classes directory.

What is WAR file with example?

In software engineering, a WAR file (Web Application Resource or Web application ARchive) is a file used to distribute a collection of JAR-files, JavaServer Pages, Java Servlets, Java classes, XML files, tag libraries, static web pages (HTML and related files) and other resources that together constitute a web ...

What consists of a WAR file?

The WAR file (Web Application Resource or Web Application ARchive) is a container for JAR files, JavaServer Pages, Java Servlets, Java classes, XML files, tag libraries, static sites (HTML and associated files), and other resources that make up an online application.


2 Answers

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

like image 160
Mike Baranczak Avatar answered Oct 23 '22 20:10

Mike Baranczak


in servlet

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

you can use this.

like image 38
Rahul Borkar Avatar answered Oct 23 '22 20:10

Rahul Borkar