See embedded answers below.

On Wed, 22 Aug 2001, Kevin HaleBoyes wrote:

> > > What should the behaviour be if I call forward() on
> > > a RequestDispatcher that (I know) doesn't exist?
> > >   (Ya, ya, I get the same behaviour as if I didn't know
> > >    it doesn't exist :-)
> > >
> > > For example:
> > >
> > >    getServletContext().
> > >       getRequestDispatcher("/I_know_this_page_doesnt_exist.html").
> > >          forward(request, response);
> > >
> > > I would expect a NullPointerException but that is not what I get.
> > > Instead, I see the following output in my browser window (Mozilla 0.92):
> > >    <html><body></body></html>
> > > Or "The document contains no data" from Netscape 4.7.
> > > And, if I use lwp-request I get a 404 error which is reasonable.
> > >
> > > What "should" I get?
> > >
> > > The javadoc for ServletContext says:
> > >    "This method returns null if the ServletContext cannot
> > >     return a RequestDispatcher for any reason."
> > > But I'm not sure what that reason might be given that a bogus URL
> > > doesn't seem to do it.  Also, the spec doesn't say anything about
> > > bad URLs.
> > >
> >
> > I *like* the interesting questions :-).
> >
> > The answer depends on a precise understanding of what the spec says.  It
> > says that you get a RequestDispatcher for the servlet that will handle
> > the request URI you specify.
>
> Ok, in the Dispatching Requests chapter (8) in Sevlet Spec v2.3 PFD 2, it
> says
>   "If no servlet can be resolved based on the given path, a
>    RequestDispatcher is provided that returns the content for that path."
>
> and that content is a 404 error.
>
> >  In Tomcat, there is an implicit mapping like
> > this:
> >
> >   <servlet-mapping>
> >     <servlet-name> default </servlet-name>
> >     <url-pattern> / <url-pattern>
> >   </servlet-mapping>
> >
> > which says, in effect, "the 'default' servlet handles *all* requests that
> > are not mapped to anything else".  Thus, there will never be a URL for
> > which you cannot return a RequestDispatcher.
>
> So that is an implementation detail.  I.E.  that is how catalina implements
> the spec.  Why does the spec allow for getRequestDispatcher to return null?

Because not every servlet container uses this technique for serving static
content.  In fact, serving static content is *not* required by the spec at
all :-).  So, even serving HTML files is a feature of Tomcat -- which you
can turn off, if you wish, by removing the servlet mapping for the default
servlet.  Then, Tomcat will indeed return null.

> Now that I've asked that, I don't actually remember seeing in the spec that
> is can return null.  Oh yeah, in the Duke Bookstore example from Sun, they
> say in the source code that getRequestDispatcher() can return null.
>

It's in the Javadocs for ServletContext.getRequestDispatcher().  In 2.3,
the Javadocs are part of the spec document itself, so they are just as
official as the rest of the text.  You can get the spec at:

  http://java.sun.com/products/servlet/download.html

> > When you actually try to RequestDispatcher.forward() to this, you will get
> > a 404 error, just as if you'd asked for it directly.
> >
> > > Thanks,
> > > Kevin.
> >
> > Craig
>
> Kevin.
>

Craig

Reply via email to