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


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY 
MATERIAL and is thus for use only by the intended recipient. If you received 
this in error, please contact the sender and delete the e-mail and its 
attachments from all computers.

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


Reply via email to