• 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

recover application direcory from bean

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can i recover the path to my application root direcory from within the managed bean?
(i'm using tomcat as application server)
thanks

eve
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AHHHHHHHHHH. Here we go again!

This is a perennial question and not just in JSF. And the answer is - you can't, because a java application is only resident in a filesystem when the application container finds it convenient to keep it in a filesystem. If the app doesn't live in a directory tree, there's no directory path to retrieve.

Java works on classpaths. A classpath is a mechanism for loading classes and class-related resources. A class can be retrieved from a file in a filesystem, from the inside of a JAR file, from a URL somewhere halfway around the planet, it can even be dynamically constructed by a program or DLL. So if you code apps thinking that you've got an actual file in an actual directory, it wouldn't be "write once, run anywhere".

Although it might seem like a pain, it's far better when you're creating apps that load and store data to designate the data storage outside of the webapp. Besides the reasons above, this also makes it possible to share the app between multiple servers (clustering) or things like run a WAR from a CD-ROM.

A good way to designate a data path in a flexible way is to define it as a property in your WEB-INF.XML file where you can do a JNDI lookup (like for "java:/comp/env/wheresmyfiles"). If you do that, not only can various deployer tools allow you to set it to whatever value you want, but most appservers have a managemenet console where you can modify it as well.

Note that for static content like images, those objects are considered classpath-loadable resources and you can refer to them by URL or get the HttpRequest object and do a getResource-type operation on them.
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic