• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Get Host appbase Path of a JSP in Tomcat

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the ServletContext.getRealPath() method.
 
Simon Klaiber
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
reply
    Bookmark Topic Watch Topic
  • New Topic