Hi Download the log4j Source from Apache. Look at the class org.apache.log4j.helpers.Loader and here you will a logic which is used to find the log4j.xml. This works for every application.
Hope this will help. On 5/17/06, Tim Lucia <[EMAIL PROTECTED]> wrote:
I have always responded with this http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html when this question comes up. It explains the most common ways to find and load resource files. It does not get specifically into servlets, however, or ServletContext.getResource[AsStream](path), which is worth knowing about. On the subject of writing configuration, I have used a database to store modifiable configuration settings (those changeable from within the app itself, such as preferences.) Otherwise, I have relied on modifying xml configuration files and/or using the Tomcat admin application. Tim -----Original Message----- From: Mark Petrovic [mailto:[EMAIL PROTECTED] Sent: Wednesday, May 17, 2006 12:02 AM To: Tomcat Users List Subject: Application configuration: how to read files from a web application A few people in the last few days on the Tomcat Users have essentially asked: How do I read a file from disk from within my web application, and furthermore, how do I declaratively configure the name of that file so as to keep its path out of my code? It's a common question, and there is a good solution that applies to any Java application, not just a web application running in a container. Here's the frequently used scheme: bundle the file of interest in your appplication's war file, and from within your application, read its contents as a resource. Assume the application's context is named myapp, the file of interest is thisfile.dat, and that you can arrange for the file to be placed in your application's WEB-INF/classes directory. When the application is unbundled by Tomcat, you should see the file in a directory listing $ cd $CATALINA_HOME $ ls webapps/myapp/WEB-INF/classes/thisfile.dat webapps/myapp/WEB-INF/classes/thisfile.dat while this is clearly Unix-like syntax, the same ideas apply to Windows. What have we done so far? Strategically place the file along the application's classpath. This is essential to the scheme: the file must be along the application's classpath - and nowhere else. This is probably worth a close read Class Loader HOW-TO: http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html Futhermore, to declaratively configure your application, you'd like to put this file path in your application configuration, which keeps it hardcoded out of your code. In other words, in the application's web.xmlfile. So among other application configuration, you put this in your web.xml <servlet> <init-param> <param-name>thefile</param-name> <param-value>/thisfile.dat</param-value> </init-param> </servlet> Finally, somewhere in your servlet, you set about retrieving the file contents as follows: String thefile = this.getInitParameter("thefile"); InputStream is = this.getClass().getResourceAsStream(thefile); That's all there is to it. Of course, all of this assumes you have a method of using an InputStream to parse or process the file, but that is generally a good assumption. -- Mark AE6RT --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
-- mfg Hans Sowa mailto:[EMAIL PROTECTED]