This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets 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
Author

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

nilk yeo
Greenhorn

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
Sheriff

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
Rancher

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 java.io.File 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...


http://learnertobeginner.blogspot.com/
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to get absolute path for web application's war file
 
Similar Threads
log4j: right path for the log file
Absolute Paths
log4j error?
Websphere 3.5, Log4j 1.2.7
Get path of file in java class in web application