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 Murach's Java Servlets and JSP this week in the Servlets 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

Get Host appbase Path of a JSP in Tomcat

Simon Klaiber

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

Joined: Jan 10, 2002
Posts: 60738

Check out the ServletContext.getRealPath() method.

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

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!
Ben Souther

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
I agree. Here's the link: http://aspose.com/file-tools
subject: Get Host appbase Path of a JSP in Tomcat
Similar Threads
Tomcat,Eclipse and form data
Name based virtual hosts
how to remove the port number in URL
tomcat as a virtual host
What is Tomcat virtual host, What is different from the Apache Tomcat?