This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Servlets and the fly likes How to get absolute path for web application's war file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to get absolute path for web application Watch "How to get absolute path for web application New topic

How to get absolute path for web application's war file

nilk yeo

Joined: Jun 07, 2010
Posts: 4
How to obtain the absolute path for my web application's war file, using HttpServletRequest
for e.g I need path as D:\apache-tomcat-5.5.28\webapps\war_file_name
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

There is nothing in the spec that returns the path to the actual war file.

If tomcat is configured to upack the war file on deployment (which is the default), using ServletContext.getRealPath ServletContext.getRealPath will return the location of the your app's directory structure. Read the docs for that method carefully though. Java web applications do not always have to be unpacked directory structures. When they're not, getRealPath will return null. This means that relying on it will make your app less portable and very dependent on the way it will be deployed.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

You can use the ClassLoader to find the location of a class file as a file, but you need to be sure to load it as a URL and file location since you can't get a location for something inside a WAR.
Once you have the URL to the WAR you can extract the first part which will be the file location of the WAR
Poobhathy Kannan
Ranch Hand

Joined: May 26, 2004
Posts: 94
Nilk, May i know why do you need to get the WAR file path? So that can think of an alternatives...
I agree. Here's the link:
subject: How to get absolute path for web application's war file
It's not a secret anymore!