Return-Path: Mailing-List: contact tomcat-dev-help@jakarta.apache.org; run by ezmlm Delivered-To: mailing list tomcat-dev@jakarta.apache.org Received: (qmail 64509 invoked from network); 28 Sep 2000 19:27:37 -0000 Received: from mercury.sun.com (192.9.25.1) by locus.apache.org with SMTP; 28 Sep 2000 19:27:37 -0000 Received: from taller.eng.sun.com ([129.144.125.34]) by mercury.Sun.COM (8.9.3+Sun/8.9.3) with ESMTP id MAA07890; Thu, 28 Sep 2000 12:26:44 -0700 (PDT) Received: from eng.sun.com (florence [129.144.251.146]) by taller.eng.sun.com (8.9.3+Sun/8.9.3/ENSMAIL,v1.7) with ESMTP id MAA09145; Thu, 28 Sep 2000 12:26:40 -0700 (PDT) Message-ID: <39D39BB4.514564B0@eng.sun.com> Date: Thu, 28 Sep 2000 12:27:48 -0700 From: "Craig R. McClanahan" X-Mailer: Mozilla 4.75 [en]C-CCK-MCD {Sony} (Win98; U) X-Accept-Language: en MIME-Version: 1.0 To: tomcat-dev@jakarta.apache.org CC: juerganbaumann@yahoo.com Subject: Re: 4.0 vs. 3.2b5 and 3.1 References: <001601c02959$4717e3b0$020aa8c0@Permit> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Spam-Rating: locus.apache.org 1.6.2 0/1000/N Juergen Baumann wrote: > I hope this is the right forum to post this finding.In order to pass > on the relative directory name to my servlet, I have the following in > my web.xml file: > resultDir > > WEB-INF/f1/resultfiles/ > > In my servlet I do:.....ServletContext context = > getServletContext(); > context.log("F1AdminServlet started");..... > virtualPath = context.getInitParameter("resultDir"); > resultDir = context.getRealPath(virtualPath);........ try { > InputStream is = new FileInputStream(resultDir + "sum.properties"); > sumProps.load(is); > is.close(); > } catch (FileNotFoundException e) { > // no sumProps available => new file will be generated > // as soon as 1. user is finished > System.out.println("No sumProps file available"); > return; > } catch (IOException e) { > System.out.println(e); > System.out.println("in F1AdminServlet"); > return; > } > ............ with 3.1 and 3.2b5 the path is: > ...WEB-INF/f1/resultfiles/sum.propertieswith 4.0m1 it seems to be: > ....WEB-INF/f1/resultfilessum.properties so the last "/" of > "WEB-INF/f1/resultfiles/" seems to be not passed on > withgetInitParameter(); pls let me know if I have to change something > in the web.xml file or what else I can do. Thanks in advanceJB In 4.0m1 it is not the getInitParameter() method that is stripping the trailing slash. That is happening in the call to getRealPath(). According to the servlet spec, the getRealPath() method is described like this: The getRealPath method takes a String argument and returns a String representation of a file on the local file system to which that path corresponds. In this case, your call equivalent to: String resultDir = context.getRealPath("WEB-INF/f1/resultfiles/"); returns "/path/to/tomcat/webapps/WEB-INF/f1/resultfiles" which, it seems to me, conforms to the spec even if it is different from Tomcat 3.1. This is a valid pathame for the resultfiles directory. As for what you can do to run portably (across all servlet containers, not just Tomcat) is add the slash yourself if the path you get back doesn't have one. But a better answer is to dispense with files completely, and do something like this instead: Properties sumProps = new Properties(); try { InputStream is = context.getResourceAsStream("/WEB-INF/f1/resultfiles/sum.properties"); sumProps.load(is); is.close(); } catch (IOException e) { ... } This will work no matter what directory your webapp is installed in. In fact, it will even work on servers that run a webapp directly out of the WAR file without expanding it -- in which case there is no such thing as a path to the properties file and getRealPath() would return null. You can still parameterize the first part of the path with a context init parameter, as you did above, if you want to. Just remember to add a slash before "WEB-INF". Craig McClanahan ==================== See you at ApacheCon Europe ! Session VS01 (23-Oct 13h00-17h00): Sun Technical Briefing Session T06 (24-Oct 14h00-15h00): Migrating Apache JServ Applications to Tomcat