To demystify things:

1. ServletContext.getResourceAsStream("/path/inside/webapp.blah")
returns an object that supports the java.io.InputStream stream
interface.  After you get the stream, you can treat it like any other
input stream in the java world.

2. The path is rooted in the *webapp*, not the file system.  Given a
file in a webapp like
$CATALINA_HOME/webapps/mywebapp/WEB-INF/myconfigs/myconfig.properties,
the path you'd use with ServletContext.getResourceAsStream() would be
"/WEB-INF/myconfigs/myconfig.properties".

3. Logs would be a different story.  You are best off configuring your
log tool according to it's documentation and let it handle the logging.

--David

David.Meldrum wrote:
> OK, I agree about writing, but what about reading?  I guess I didn't
> realize there was a method called getResourcesStream(), on
> ServletContext, so that clears up some confusion.  Yet when I read the
> javadoc on ServletContext.getRessourceAsStream()
> see:
> http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#getResourceAsStream(java.lang.String)
> <http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#getResourceAsStream%28java.lang.String%29>
>
> It still isn't clear how to properly use this.  For example, how do I
> get a reference to the "/webapps/MyWebApp/WEB-INF/foo.properties" file?
> Like this?
> ServletContext.getRessourceAsStream("/webapps/MyWebApp/WEB-INF/foo.properties");?
>
> The javadoc says the argument is a path, but I don't know the full
> path, only relative to Tomcat, and I don't really know where Tomcat is.
> Also how do I create a file under "/logs"?
> Is there some way to determine the path to the /logs directory in a
> format that I can create a file under the logs
> directory, without assuming where Tomcat is deployed?
>
> -d
>
> Caldarale, Charles R wrote:
>>> From: David.Meldrum [mailto:david.meld...@verizon.net] Subject: Re:
>>> Path problem
>>>
>>> actually in my case I am trying to read/write from a ContextListenr
>>>     
>>
>> It's generally a bad idea to ever *write* into the webapp deployment
>> space.  You have no guarantee that the space is writable, nor that
>> the container will provide any write access.  You're much better off
>> writing to files outside of Tomcat's directory structure, where the
>> path can be provided by system property, environment variable,
>> <init-param>, or whatever.
>>
>>  
>>> As recommended below, you could use Class.getResourcesStream(), but
>>> as I understand it,
>>> that will give the location of this class under WEB-INF.
>>>     
>>
>> Or anywhere else in the webapp's structure; it's not confined to
>> WEB-INF.
>>
>>  
>>> public void contextInitialized(ServletContextEvent event)  {
>>>     ServletContext sctx = event.getServletContext();
>>>     String propPath = sctx.getRealPath(
>>> "/WEB-INF/resource.properties");
>>>     FileInputStream inStrm = new FileInputStream(propPath);
>>>   .....
>>> }
>>>     
>>
>> Using ServletContext.getRealPath() is risky; the container is under
>> no obligation to provide access to the underlying file system (think
>> deployment via .war file).  Much safer and better to use
>> getResourceAsStream().
>>
>>  - Chuck
>>
>>


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

Reply via email to