tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jacob Kjome" <Jacob.R.Kj...@syntegra.com>
Subject RE: Location of application-specific configuration files
Date Thu, 21 Dec 2000 17:40:41 GMT
Here is a solution for non-platform specific reading of files whether
they be directly on the file system or in a .jar archive:
 
 
Note:  This example utilizes the java.util.Properties class.  You can do
whatever you need, though.  The technique should be pretty much the same
Properties props = new Properties();

try {

    InputStream propsStream =
this.getClass().getResourceAsStream("my.properties");

    if (propsStream != null) {

        props.load(propsStream);

        propsStream.close();

    }

} catch (IOException e) {

    System.err.println("Caught IOException: " + e.getMessage());

}

That was doing it with a normal class file (not a servlet).  Now for a
servlet example:

 

To read file "foo.txt" from the document root, try this:

InputStream is = getServletContext().getResourceAsStream("/foo.txt");

If you want to read it as characters, you can wrap an InputStreamReader
around it:

InputStreamReader isr = new InputStreamReader(is);

This works no matter what directory your webapp runs in. As an added
bonus, it

will also work in servlet containers that don't store your webapp as a
directory

at all (for example, if it runs the app directly out of a WAR file).

 

Thanks go to Craig McClanahan for pointing both of these examples out to
me.

Hope that helps!

Jake

-----Original Message-----
From: Guillaume Bilodeau [mailto:gbilodeau@videotron.ca]
Sent: Thursday, December 21, 2000 11:01 AM
To: tomcat-user@jakarta.apache.org
Subject: Location of application-specific configuration files


Hi,
 
I'm not sure if this is the right place to post this kind of message.
If it's appropriate, then sorry for taking your time.
 
I have developed a Web application using the Java Servlet 2.2 API and
following a MVC architecture; I am testing using Tomcat 3.1. My servlets
use a singleton DatabaseManager to access an SQL database. This class'
constructor reads the JDBC driver and URL from a configuration file
(database.xml).
 
Where should I put this file? And what path should I use to access it?
 
Currently, the file is in \tomcat\bin and the FileInputStream uses the
"database.xml" path. I would like this file to be in the webapps\myapp
directory. Since the DatabaseManager isn't a servlet, it doesn't have
access to virtual-to-real path translation. Any ideas?
 
 
Thanks in advance! 
Guillaume
 
~~~~~
Guillaume Bilodeau
gbilodeau@videotron.ca <mailto:gbilodeau@videotron.ca> 
http://pages.infinit.net/bilodg <http://pages.infinit.net/bilodg> 
 
"Only two things are infinite, the universe and human
 stupidity, and I'm not sure about the former."
     (Albert Einstein)
 


Mime
View raw message