tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From JensStu...@quidinfo.it
Subject Re: (argh!) RE: Location of application-specific properties files
Date Wed, 07 Aug 2002 07:02:57 GMT

I did never intend, that you have to include the WEB-INF or classes part
into the path. The servlet spec says, that the folder "WEB-INF/classes"
will be mounted into the application classpath as well as every jar in
"WEB-INF/lib". So a properties file named MyProp.properties placed in the
"WEB-INF/classes" folder (or in any of the mounted jars) will be found from
inside your application with the simple path "MyProp" or if you use
getResourceAsStream() with "MyProp.properties". No folders, no magic, no
servlet dependency. It is the same process like finding a class - and a
class should never reside directly in the "WEB-INF" folder.

Regards,

Jens Stutte



                                                                                         
           
                    Jacob Kjome                                                          
           
                    <hoju@visi.co        To:     "Tomcat Users List"                  
              
                    m>                   <tomcat-user@jakarta.apache.org>       
                    
                                         cc:                                             
           
                    07/08/2002           Subject:     Re: (argh!) RE: Location of        
           
                    05.49                application-specific properties files           
           
                    Please                                                               
           
                    respond to                                                           
           
                    "Tomcat Users                                                        
           
                    List"                                                                
           
                                                                                         
           
                                                                                         
           





Do you literally have the filename as
"/WEB-INF/classes/myprops.properties"?

The only thing that knows about that is the ServletContext.  If you were to

use that method you'd be better off storing your config file in
"WEB-INF" and doing:

load(getServletContext().getResourceAsStream
("/WEB-INF/myprops.properties"))


Since you want to avoid this so that your app is not servlet specific, you
can do the following....

load(CurrentClass.class.getResourceAsStream("/myprops.properties"));

or

load(CurrentClass.class.getClassloader().getResourceAsStream
("myprops.properties"));

or

load(Thread.currentThread().getContextClassLoader().getResourceAsStream
("myprops.properties"));


Notice in all the cases where you are looking in the classpath/classloader,

you do not reference "WEB-INF" or the "classes" directory within
WEB-INF.  The classloader doesn't know about those.  The ServletContext, on

the other hand, knows about everything inside the root of the current
webapp.

That should solve things.

Jake

At 09:22 PM 8/6/2002 -0400, you wrote:
>Jens,
>
>Thanks for the reply.  I understand what you're saying, and I wish it was
>working for me, but it doesn't.  The ONLY way I've been able to get this
to
>work is by using the servlet context method as mentioned in a previous
post,
>but that's NOT the way I want this to ultimately work.
>
>I want to make it as generic as possible without having to rely on passing
>in a servlet context to my class.  For example, what if I need access to
>that file in an application that's not using a servlet?
>
>If I have the filename as "/WEB-INF/classes/myprops.properties", I have
>tried this in one of four different ways so far:
>
>1. load(new FileInputStream(filename));
>2.
>load(Thread.currentThread().getContextClassLoader
().getResourceAsStream(file
>name));
>3. load(this.getClassLoader().getResourceAsStream(filename));
>4. load(getServletContext().getResourceAsStream(filename));
>
>All of them fail for not finding the file except #4, but that's not the
>optimal solution as mentioned previously.
>
>As an aside, I pulled the "java.class.path" from the System properties to
>see where it's pointing, and all it has in it is
>"<CATALINA_HOME>\bin\bootstrap.jar" with no other paths included.
>
>Thanks,
>Bob
>
>
>-----Original Message-----
>From: JensStutte@quidinfo.it [mailto:JensStutte@quidinfo.it]
>Sent: Tuesday, August 06, 2002 9:56 AM
>To: Tomcat Users List
>Subject: Re: Location of application-specific properties files
>
>You must load your properties through some Classloader that is able to
>locate your properties file.  For example you can use the class
>PropertyResourceBundle for a real properties file or if you want to access
>some own file format, just call the Thread.getCurrentThread
>().getContextClassLoader().getResourceAsStream(...) function. Then the
>WEB-INF/classes folder or some of your application specific .jar files in
>WEB-INF/lib (if any) is the right place for it (and as far as i know, it
is
>the only standard way, which assures you to find a specific file on every
>standard J2EE server).
>
>
>
>--
>To unsubscribe, e-mail:   <
mailto:tomcat-user-unsubscribe@jakarta.apache.org>
>For additional commands, e-mail: <
mailto:tomcat-user-help@jakarta.apache.org>





--
To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-user-help@jakarta.apache.org>


Mime
View raw message