Meaningless Drivel is fun!
The moose likes JSF and the fly likes Getting the path for data files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Getting the path for data files" Watch "Getting the path for data files" New topic

Getting the path for data files

Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 155
Thanks for the reply on the JAR files in a previous thread. That problem has been fixed for now.

I have another question. How do you write to or read from data files in a JSF project an Eclipse? I want to have a folder called, say data, immediately below the project folder, into which I can read and write files. If I use dir.getCanonicalPath() or System.getProperty("user.dir") to find the location of the default folder, I found that it is in the config folder of the Glassfish server I am using with Eclipse and JSF. This is not what I want.

In using Google I found examples using code like:

ServletContext servletContext = (ServletContext) externalContext.getContext();
String webContentRoot = servletContext.getRealPath("/");

but I can never get something like that to work, because either an abstract class is used or an interface.

An example of my code is here:

In this particular case it is complaining that getCcontext() is undefined. Various attemps to sort this out have so far failed, and I have spent quite a lot of time on this, so some help would be most appreciated. I am sure there is something that has to be done correctly concerning how ServletContext must be declared correctly.

This is a JSF 1.2 application that I want to modify, before moving to JSF 2.0.

Your help for this would be most apprectiated.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

Don't store data files inside your webapp. It's a violation of the J2EE standard, won't work on all webapp servers, and when it does, sooner or later, it will bite you. Use an independent directory for data that's located somewhere outside both the webapp (WAR) and the webapp server.

An IDE is no substitute for an Intelligent Developer.
Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 155
OK, many thanks for your helpful advice. I am now instead going to use a folder in my desktop using System.getProperty("user.home") to get the path, then appending \Desktop and \Folder-Name to store the data files as a temporray measure for development and testing. Eventually this will be changed when the code will be moved onto an online server.
I agree. Here's the link:
subject: Getting the path for data files
It's not a secret anymore!