aspose file tools*
The moose likes Java in General and the fly likes properties file, default location Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "properties file, default location" Watch "properties file, default location" New topic
Author

properties file, default location

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Just a quicky...

When creating a webapp folder structure (web, WEB-INF, META-INF, class, lib, etc) is there a default location for property files?

Looking at a DB Conn Pool and the values of the DB (user, pwd, driver, etc) are in a property file, and the line that calls the file is:
InputStream is = getClass().getResourceAsStream("/dbconn.properties")

Is there a default location for peoperty files, as I don't really want the absolute path here.

Cheers
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
The files / resources that you want to read using the web application's classloader can be put in classes folder within WEB-INF.

If you want to put your configuration file is some other location .Say you have lots of configuration files for your application and you want to have a separate config directory within WEB-INF for storing all the configuration files ,then use your servletcontext's getResourceAsStream method with the "/WEB-INF/config/app.properties" as parameter to it.

Hope this helps,
[ August 02, 2007: Message edited by: Rahul Bhattacharjee ]

Rahul Bhattacharjee
LinkedIn - Blog
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
So basically by default 'it' looks under the /WEB-INF/classes folder?
But if I want a specific folder I can call something like:
/WEB-INF/properties/...

So:
InputStream is = getClass().getResourceAsStream("/WEB-INF/props/dbconn.properties");

Excelent, just what I needed, I just didn't want to have to put the whole C:/X/Y/Y/1/2/3/... path in.

Cheers!

:-)
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Do not use

InputStream is = getClass().getResourceAsStream("/WEB-INF/props/dbconn.properties");

use

ServletContext sc = getServletContext();
InputStream is = sc.getResourceAsStream("/WEB-INF/props/dbconn.properties");

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
But it's not a Servlet calling the file, just a DBConnManager for connection pooling
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Keith Wilson:
But it's not a Servlet calling the file, just a DBConnManager for connection pooling


I guess you can do quite a few things for this issue.

Write a singleton and initialize it in the start up servlet with serveltContext as one of the parameters.If you do not have a start up servlet in your application, then you might want to write a context listener for this.Which would lick off the loading the singleton on initialization of the application context.Now this already initialized singleton would be accessible to the DBManger class too and it can use the values from the singleton.

Singleton should use servletContext to read the config file to a properties object.Write a method which would accept a key and return the value after looking up it in the already loaded properties object.
[ August 02, 2007: Message edited by: Rahul Bhattacharjee ]
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Think I understnd,

Cheers for that
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: properties file, default location