Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

properties file, default location

 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it's not a Servlet calling the file, just a DBConnManager for connection pooling
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think I understnd,

Cheers for that
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic