• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
nilk yeo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nilk, May i know why do you need to get the WAR file path? So that can think of an alternatives...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic