tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Larry Meadors" <Larry.Mead...@plumcreek.com>
Subject RE: Quick Question
Date Wed, 14 Aug 2002 16:22:15 GMT
It is  little more work to get read/write access, but the getResource()
method will give you a URL object.

You can then use URL.getProtocol() to see if it is coming from a file or
something else (a jar or even http, if you use the URLClassLoader), then
use the URL.getFile() if it was a file to get the path to the file.

Might be a good idea if you want to do that to create a 
class that makes this more abstract so you have a single API to work
with instead of trying to remember all the ClassLoader/URL/File APIs...

I might just do that - it might be fun. ;-)

Larry

>>> David.Durham1@scott.af.mil 08/14/02 09:45 AM >>>
good point, although if you need to write to the 
file, getResourceAsStream doesn't work.  Is there
something else along the same lines that will?


> -----Original Message-----
> From: Larry Meadors [mailto:Larry.Meadors@plumcreek.com]
> Sent: Wednesday, August 14, 2002 10:11 AM
> To: tomcat-user@jakarta.apache.org
> Subject: RE: Quick Question
> 
> 
> IMO, it seems like a bad idea to me to tie your bean 
> code to a servlet context unless you REALLY need to.
> 
> To me, a better way would be to put the file in a 
> directory under classes, and use the classloader of 
> the current thread to get to the file.
> 
> This way, you do not need servlet.jar to use your bean 
> if you every decide to use it outside of a servlet.
> 
> This is a very simple process. Here is an example:
> 
> private InputStream getFile(String name){
>  return Thread.
>   currentThread().
>   getContextClassLoader().
>   getResourceAsStream(name);
> }
> 
> To read a properties file classes/myprops.properties 
> for instance, you just do this:
> 
> Properties p = new Properties();
> p.load(getFile("myprops.properties"));
> 
> Larry
> 
> >>> David.Durham1@scott.af.mil 08/14/02 08:42 AM >>>
> It will work in a bean if you pass a reference 
> to the servlet context to it...
> 
> --
> 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>



--
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