tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Smith <david.sm...@cornell.edu>
Subject Re: loading properties file from WEB-INF instead of WEB-INF/classes
Date Sun, 18 Apr 2010 18:27:35 GMT
An alternative exists in the ServletContext class --

ServletContext.getResource( path ) takes path as relative to the current
webapp and returns a URL for opening the resource or null if the
resource was not found.

also there is --

ServletContext.getResourceAsStream( path ) takes a path as relative to
the current webapp and returns an i/o stream for reading the file.

For example, using this within a servlet:

try {
    java.io.InputStream res_stream =
this.getServletContext().getResourceAsStream('/WEB-INF/my-config.ini') ;
    // read the data
} catch (java.io.IOException ioe) {
    // handle the problem
}

both of these are well documented in the servlet spec.

--David

On 4/19/2010 2:02 PM, Yucca Nel wrote:
> props file can be placed in src package and then placed along with classes 
> in the Web-INF directory when compiling with the destination flag .This 
> method has been used since jdk 1 for internationalization and is understood 
> by new and old devs. On a side not perhaps someone can help me with a 
> related question regarding how we intwenationalize images? or a link:P
>
> --------------------------------------------------
> From: "Harry Metske" <harry.metske@gmail.com>
> Sent: Sunday, April 18, 2010 7:32 PM
> To: "Tomcat Users List" <users@tomcat.apache.org>
> Subject: Re: loading properties file from WEB-INF instead of WEB-INF/classes
>
>   
>> 2010/4/17 Thufir <hawat.thufir@gmail.com>
>>
>>     
>>> getPropsFromWebINF works so long as the properties file is within the
>>> package of
>>> the class (/.  However, I'd like to put the properties file under 
>>> WEB-INF:
>>>
>>>       
>>
>>     
>>>    public void getPropsFromWebINF() throws IOException {
>>>        Properties p = new Properties();
>>>        InputStream is;
>>>        is = getClass().getResourceAsStream("sqljdbc4.properties");
>>>        p.load(is);
>>>        log(p.toString());
>>>    }
>>>
>>>       
>> Class.getResourceAsStream() will delegate to the classloader to find the
>> resource (searches the classpath) , see
>> http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)
>> The tomcat site has an excellent description of Tomcat's classloading :
>> http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html , this
>> should bring you to the conclusion that the above won't work.
>> You should put your sqljdbc4.properties file in WEB-INF/classes, or in
>> WEB-INF/lib if packaged in a jar file.
>> If you reference it from your application, make sure you prepend a "/" (or
>> it will search in your servlet's package) .
>>
>>
>>
>> I've seen mention of using:
>>     
>>> System.getenv("APP_PROPERTIES");
>>>
>>> in conjunction with a context (context.xml under META-INF) along the 
>>> lines
>>> of:
>>>
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <Context antiJARLocking="true" path="/A00720398sat">
>>>    <Environment name="APP_PROPERTIES"
>>>                 description="The APP Properties File" override="false"
>>>                 type="java.lang.String"
>>>                 value="/WEB-INF/app.properties" />
>>> </Context>
>>>
>>>
>>> however, I'm not able to get the environment, I just get a null value.
>>>
>>>
>>>       
>> can you be a bit more specific where you get a null value (show some lines
>> of code)...
>> although the above won't bring you to the content of the property file, 
>> you
>> only have defined a key/value pair.
>>
>>
>> regards,
>> Harry
>>
>>
>>     
>>> What's the correct, and simple, idiom?  (Staying away from jndi and dbcp
>>> for
>>> now, and, oddly enough, servlets in this case.)
>>>
>>>
>>>
>>> Actually, I suppose in a sense it's in WEB-INF:
>>>
>>> dtc01l0376-06:~ a00720398$
>>> dtc01l0376-06:~ a00720398$ jar -tfv
>>> NetBeansProjects/A00720398sat/dist/A00720398sat.war | grep sql
>>>   498 Sat Apr 17 14:36:14 PDT 2010
>>> WEB-INF/classes/controller/sqljdbc4.properties
>>>
>>>
>>>
>>> however, I'd like to move it from WEB-INF/classes up to just WEB-INF.
>>>
>>>
>>>
>>> thanks,
>>>
>>> Thufir
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>
>>>
>>>       
>>     
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message