Return-Path: Mailing-List: contact tomcat-user-help@jakarta.apache.org; run by ezmlm Delivered-To: mailing list tomcat-user@jakarta.apache.org Received: (qmail 47539 invoked from network); 13 Dec 2000 22:06:08 -0000 Received: from lukla.sun.com (192.18.98.31) by locus.apache.org with SMTP; 13 Dec 2000 22:06:08 -0000 Received: from centralmail1.Central.Sun.COM ([129.147.62.10]) by lukla.Sun.COM (8.9.3+Sun/8.9.3) with ESMTP id PAA06434 for ; Wed, 13 Dec 2000 15:06:07 -0700 (MST) Received: from esun1as-mm. (esun1as-mm.Central.Sun.COM [129.147.34.144]) by centralmail1.Central.Sun.COM (8.9.3+Sun/8.9.3/ENSMAIL,v1.7) with SMTP id PAA22347 for ; Wed, 13 Dec 2000 15:06:07 -0700 (MST) Received: from eng.sun.com by esun1as-mm. (SMI-8.6/SMI-SVR4) id PAA28565; Wed, 13 Dec 2000 15:20:05 -0700 Message-ID: <3A37F326.1E561D51@eng.sun.com> Date: Wed, 13 Dec 2000 14:07:34 -0800 From: "Craig R. McClanahan" X-Mailer: Mozilla 4.76 [en]C-CCK-MCD {Sony} (Win98; U) X-Accept-Language: en MIME-Version: 1.0 To: tomcat-user@jakarta.apache.org Subject: Re: Current the context from a servlet - to open a file? References: <3A37EE48.FBCDFC31@terristech.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Spam-Rating: locus.apache.org 1.6.2 0/1000/N Jeff Taylor wrote: > When running a servlet (located in webapps/myapp/WEB-INF/classes) I'd > like to open a ascii file (located in webapps/myapp). What's the best > way of obtaining the current Context -- so as not to hardcode it in? Is > there an appropriate class/method to call? Thanks in advance! > To read file "foo.txt" from the document root, try this: InputStream is = getServletContext().getResourceAsStream("/foo.txt"); If you want to read it as characters, you can wrap an InputStreamReader around it: InputStreamReader isr = new InputStreamReader(is); This works no matter what directory your webapp runs in. As an added bonus, it will also work in servlet containers that don't store your webapp as a directory at all (for example, if it runs the app directly out of a WAR file). > > Jeff Craig McClanahan