tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peter Crowther <>
Subject RE: where to put config file in a webapp
Date Wed, 02 Apr 2008 12:08:17 GMT
> From: Kimberly Begley []
> Great thanks - it's not actually in a servlet - just a java
> class of methods
> so I guess I could pull it out of the java class and put it
> into the servlet
> that is calling the method - if that makes sense - I was just
> hoping to avoid that.

Can we be clear about what "it" is here?  Lots of pronouns, no clarity :-).

If I read you correctly:

1. You have a Java class that reads a config file, presently from a hard-coded location;

2. Other code within your webapp invokes methods on the Java class that reads the config file
in order to read configuration information;

3. You want the Java class to be sensitive to the webapp's location, so that the class can
read the file from a location within the webapp;

4. You don't want to put webapp-specific code into the Java class that reads the config file.

While you can't *quite* do both 3 and 4, the following approach might be helpful:

- Amend the class that reads the config file so that it can accept a stream, possibly in a
constructor or static method call - depends whether you're instantiating the class.  Read
the config data, close the stream - you don't want to hold a file stream open for longer than
you have to!

- In your servlet code, use Chuck's init parameter approach to obtain the path to the config
file.  Open a stream using getResourceAsStream() and hand that stream to the class that reads
the config file.

- If you still want your class that reads the config file to be usable in non-webapps, re-code
so that if the class hasn't been passed a stream by the time the first config variable is
requested, it loads from a default location.

This isolates your class for reading the config file from any servlet dependencies (they're
external) at the expense of a little more complexity.

                - Peter

To start a new topic, e-mail:
To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message