You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Reynir Hübner <re...@hugsmidjan.is> on 2002/12/17 20:35:02 UTC

- mail tests AUBUCHON, KEVIN

Yes it works fine.

Hope it helps
-reynir


> -----Original Message-----
> From: AUBUCHON, KEVIN (SBCSI) [mailto:ka3751@sbc.com] 
> Sent: 17. desember 2002 19:28
> To: Tomcat Users List
> Subject: RE: File access from a servlet.
> 
> 
> test message - sorry
> 
> -----Original Message-----
> From: Tim Moore [mailto:tmoore@blackboard.com]
> Sent: Tuesday, December 17, 2002 1:11 PM
> To: Tomcat Users List
> Subject: RE: File access from a servlet.
> 
> 
> > -----Original Message-----
> > From: Patrick Martz [mailto:PMartz@teltone.com]
> > Sent: Tuesday, December 17, 2002 2:02 PM
> > To: 'Tomcat Users List'
> > Subject: RE: File access from a servlet.
> > 
> > 
> > Ok well that's exactly the problem. getResourceAsStream
> > requires you to supply the path of the resource and that is 
> > what I'm missing. I did a quick look at ServletContext and 
> > iterated through the attributes and found none that seemed to 
> > give me what I want. These are the attributes currently
> > defined:
> > 
> > org.apache.catalina.jsp_classpath javax.servlet.context.tempdir
> > org.apache.catalina.resources
> > org.apache.catalina.WELCOME_FILES
> > 
> > None of which seems to be what I'm looking for...essentially
> > something that will tell me the path of my current context so 
> > I can modify that path to access my data file. :)
> > 
> 
> But the path is relative to the root of the context, so you don't need
> to know the path to the context, just where the file is 
> within it.  For
> example, if your file is actually in
> /usr/local/tomcat/webapps/myapp/data/blah.dta, you would call
> ServletContext.getResourceAsStream("/data/blah.dta").
> 
> If you *really* want the path of the context root, you can use
> ServletContext.getRealPath("/") (or
> ServletContext.getRealPath("/data/blah.dta") to get the 
> file's path) but
> that won't work with WARs, and shouldn't really be necessary.
> 
> 
> I just reread your message and realized that you said that the data is
> stored in the directory with the servlet class file.  Maybe 
> you can use
> getClass().getResourceAsStream("blah.dta")?
> -- 
> Tim Moore / Blackboard Inc. / Software Engineer
> 1899 L Street, NW / 5th Floor / Washington, DC 20036
> Phone 202-463-4860 ext. 258 / Fax 202-463-4863
> 
> 
> > 
> > 
> > -----Original Message-----
> > From: Tim Moore [mailto:tmoore@blackboard.com]
> > Sent: Tuesday, December 17, 2002 10:46 AM
> > To: Tomcat Users List
> > Subject: RE: File access from a servlet.
> > 
> > 
> > > -----Original Message-----
> > > From: Patrick Martz [mailto:PMartz@teltone.com]
> > > Sent: Tuesday, December 17, 2002 1:42 PM
> > > To: 'tomcat-user@jakarta.apache.org'
> > > Subject: File access from a servlet.
> > > 
> > > 
> > > Hi all.
> > > 
> > > I'm currently working on a java servlet with tomcat and I
> > > want it to be able to load a different data file dependent on 
> > > certain parameters passed to the servlet. The problem is that 
> > > if I just try to open the file with the file name (i.e. 
> > > FileInputStream fin = new FileInputStream("blah.dta");) it 
> > > fails to find the file. I am guessing this is because the 
> > > runtime directory is different from the directory the servlet 
> > > is running in? (the data file and the servlet are in the same 
> > > directory, but the servlet fails to find the file still). So 
> > > my question is, is there a way to get the current runtime 
> > > directory for Tomcat so that I can perhaps supply a relative 
> > > path to get to the file and have the servlet be able to open 
> > > it? Thanks!
> > > 
> > > Patrick
> > > 
> > > P.S. For debugging purposes I HAVE tested opening of the file
> > > from a stub class and it works just fine that way, but fails 
> > > from the servlet.
> > > 
> > 
> > Rather than using FileInputStream, try
> > ServletContext.getResourceAsStream.  This is the preferred 
> method for
> > accessing files within your webapp.  You pass in the path 
> relative to
> > the context root directory.  An added bonus is that this will 
> > still work
> > if you deploy your webapp as a WAR.
> > 
> http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/Se
rvletConte
xt.html#getResourceAsStream(java.lang.String)
-- 
Tim Moore / Blackboard Inc. / Software Engineer
1899 L Street, NW / 5th Floor / Washington, DC 20036
Phone 202-463-4860 ext. 258 / Fax 202-463-4863

--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>

--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>