On Tue, 21 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. 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.
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