tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul Kofon" <pko...@hotmail.com>
Subject Re: Strange NullPointerException Error in t4.0b7
Date Thu, 01 Jan 1970 00:00:00 GMT
Hi Craig,
Thanks for the insight. I know I can't assume that all application servers 
will have a webapps directory so actually, I have a properties file that 
provides the absolute path (depending on the platform) to the "home" 
directory of my web application. However, before I apply any configurations, 
I run some tests using hard-coded values. That's why I came up with the
not-so-portable "../webapps/humantrack/forms". And I was surprised to find 
that a working application wasn't working any more after I changed servers, 
even though their directory structures are identical.
Anyway, I guess I'll just move on to the the Servelt 2.3 spec (good thing 
T4.0 supports it) and use the tip you suggested.

Regards,

Paul



>From: "Craig R. McClanahan" <craigmcc@apache.org>
>Reply-To: tomcat-user@jakarta.apache.org
>To: <tomcat-user@jakarta.apache.org>
>Subject: Re: Strange NullPointerException Error in t4.0b7
>Date: Tue, 4 Sep 2001 08:49:37 -0700 (PDT)
>
>
>
>On Tue, 4 Sep 2001, Paul Kofon wrote:
>
> > Date: Tue, 04 Sep 2001 12:03:45
> > From: Paul Kofon <pkofon@hotmail.com>
> > Reply-To: tomcat-user@jakarta.apache.org
> > To: tomcat-user@jakarta.apache.org
> > Subject: Strange NullPointerException Error in t4.0b7
> >
> > Hi All,
> > I recently downloaded and installed Tomcat 4.0 b7. I found it to be 
>quite
> > responsive and remarkably faster than it's predecessors. However, my joy 
>was
> > short-lived when I found out an application which I had written and
> > extensively tested under Tomcat 3.2.2. was not working in the T4.0 b7
> > environment - I kept getting a NullPointerException error. I snooped 
>around
> > a bit and found out that the following piece of code was the culprit:
> >
> > File directory = new File("../webapps/humantrack/forms");
> > File files[] = directory.listFiles();
> >
> > Now, here's the strange thing: "directory" isn't null but the array,
> > files[], is! This code works without change in T3.2.2 as files[] doesn't
> > return a null value. What's wrong?
> >
>
>Your pathname ("../webapps/humantrack/forms") is relative, so it will be
>resolved against the current working directory of the servlet container --
>something that you should *not* be making any assumptions about.
>
>You can solve your immediate problem with something like this ...
>
>   String contextRoot =
>     getServletContext().getRealPath("/");
>   if (contextRoot != null) {
>       File directory = new File(contextRoot + "/../humantrack/forms");
>       File files[] = directory.listFiles();
>       ...
>   } else {
>      ... error message ...
>   }
>
>but even this will not work *unless* all your web applications are stored
>underneath the "webapps" directory.  In general, you can make *no*
>assumptions about this.
>
>A more rigorous approach will often work (assuming that "/humantrack" is
>the context path for a different web app):
>
>   ServletContext sc = getServletContext().getContext("/humantrack/forms");
>   if (sc != null) {
>       File directory = new File(sc.getRealPath("/forms"));
>       File files[] = directory.listFiles();
>       ...
>   } else {
>       ... error message ...
>   }
>
>but even this is not guaranteed, because servlet containers are allowed to
>return null from ServletContext.getContext().
>
>And *all* of the solutions above assume that you are running your web
>applications from open directory structures, rather than from WAR files.
>This is a common feature of containers, but is not guaranteed to be
>available universally.  So, a new feature of the Servlet 2.3 API lets you
>ask for URLs of resources within a web application in a portable way
>(again, assuming that "/humantrack" is the context path for your other
>app):
>
>   ServletContext sc = getServletContext().getContext("/humantrack/forms");
>   Set paths = sc.getResourcePaths("/forms/");
>
>and you can iterate through the paths.  Each of them will be a String that
>you can use in calls like:
>
>   InputStream is = sc.getResourceAsStream(path);
>
>
> > Regards,
> >
> > Paul
> >
>
>Craig McClanahan
>


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp


Mime
View raw message