Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get Host appbase Path of a JSP in Tomcat

 
Simon Klaiber
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have following problem:

I have a Webapplication and I need refer to a file that is in the appBase directory of a virtual host under Tomcat.

So under standard configuration it would be under <tomcatdir>/webapps

Bbut under a configuration with virtual hosts (tomcat standalone) it could also be in the appBase directory of this virtual host.
So if the Host has the appBase "XYZ" it would be under <tomcatdir>/XYZ

When I refer to a file from the jsp the basepath is <tomcatdir>/bin to i would have to refer to "../webapps/file.dat" or "../XYZ/file.dat" or generic "../<appBase>/file.dat"

My Problem now is, that
- I don't know what the name of the appbase will be
- I can't put a configuration in thw Web.xml deployment descriptor because the people who deploy it will just drop the WAR in the appbase directory.
- I can't put the DFile at a place blow the appBase Directory because there will be several instances of this Probram on the same Machine in different Host directories

How can I get the appBase from the JSP at runtime?


Thanks Simon
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64188
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the ServletContext.getRealPath() method.
 
Simon Klaiber
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!

This is a working solution.

It might be not the most elegant way since the file i'm looking for is in the appBase Directory directly and has no URL but I can get the path of the JSP and drill down two directories and get the appBase Dir this way.

Thanks a lot again!
Simon
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing to keep in mind is that getRealPath will return null if your app isn't being deployed from an exploded file system (being deployed as a packed war file).
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic