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

recover application direcory from bean  RSS feed

 
eve agostini
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • 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
 
Tim Holloway
Bartender
Posts: 18713
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!