wood burning stoves 2.0*
The moose likes JSP and the fly likes Get Host appbase Path of a JSP in Tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Get Host appbase Path of a JSP in Tomcat" Watch "Get Host appbase Path of a JSP in Tomcat" New topic
Author

Get Host appbase Path of a JSP in Tomcat

Simon Klaiber
Greenhorn

Joined: Nov 13, 2003
Posts: 9
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

Joined: Jan 10, 2002
Posts: 61082
    
  66

Check out the ServletContext.getRealPath() method.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Simon Klaiber
Greenhorn

Joined: Nov 13, 2003
Posts: 9
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

Joined: Dec 11, 2004
Posts: 13410

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


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Get Host appbase Path of a JSP in Tomcat