tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <Craig.McClana...@eng.sun.com>
Subject Re: <context-param> 4.0 vs. 3.2b5 and 3.1
Date Thu, 28 Sep 2000 19:27:48 GMT
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: <context-param>
>         <param-name>resultDir</param-name>
>         <param-value>
>          WEB-INF/f1/resultfiles/
>         </param-value>
>     </context-param> 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 <http://www.apachecon.com>!
Session VS01 (23-Oct 13h00-17h00):  Sun Technical Briefing
Session T06  (24-Oct 14h00-15h00):  Migrating Apache JServ
                                    Applications to Tomcat



Mime
View raw message