tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <craig...@apache.org>
Subject Re: RequestDispatcher.forward() ?
Date Mon, 12 Nov 2001 06:09:48 GMT


On Sun, 11 Nov 2001, Vladimir Grishchenko wrote:

> Date: Sun, 11 Nov 2001 18:30:37 -0800
> From: Vladimir Grishchenko <vladgri@hotmail.com>
> Reply-To: Tomcat Users List <tomcat-user@jakarta.apache.org>
> To: tomcat-user@jakarta.apache.org
> Subject: RequestDispatcher.forward() ?
>
> Hi there,
>
> Could somebody tell me if this behavior by design and if so why:
>
> The following fragment forwards a request to main application page:
>
> //-----------------------
> String main = "/index.jsp";
> //String main = "/";
>
> RequestDispatcher dispatcher
>     = getServletContext().getRequestDispatcher(main);
> dispatcher.forward(req, resp);
> //-----------------------
>
>
> Forwarding works on server side if code being executed as shown (main is
> "/index.jsp") so client isn't aware about it. However, if main is set to be
> "/" (commented line above), 302 is sent to the client, so it's not a
> server-side redirect, right? In this case response "Location" header is set
> to http://host/pathtowebapp/index.jsp. Am I missing something? Tomcat was
> able to find out that index.jsp is a welcome file but nonetheless it's a
> client side redirect. Not a big problem of course, just curious...
>

The reason has to do with how Tomcat happens to implement things.  Let's
walk through what is really happening to make sense of it:

* You ask for a dispatcher for path "/".

* Tomcat resolves this to the default file-serving servlet
  (unless you have done something really strange to your configuration).

* The default file-serving servlet treats a path of "/" as a
  request for the appropriate welcome file for this web application,
  so it looks up the right path and issues an HTTP redirect (302).

Try a request dispatcher for "/index.html" (or "/index.jsp", or whatever
is appropriate for your application), and you will find that it's all done
on the server side.  The "/" path will explicitly ask for a redirect,
which is why you are seeing one.

> regards,
> Vlad.
>

Craig McClanahan


--
To unsubscribe:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-help@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-owner@jakarta.apache.org>


Mime
View raw message