You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Vladimir Grishchenko <vl...@hotmail.com> on 2001/11/12 03:30:37 UTC

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...

regards,
Vlad.



--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: RequestDispatcher.forward() ?

Posted by "Dr. Evil" <dr...@sidereal.kz>.
> 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...

You have to be clear about what you mean when you say "forward".
There is "send a redirect" or "get a RequestDispatcher", which are
different things.  What the fragment above does is it tries to forward
to a servlet which is the resources associated with /.  That doesn't
exist so it sends an error.  If you want to send something to the
client you need to use response.sendRedirect() I believe.

--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: RequestDispatcher.forward() ?

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Sun, 11 Nov 2001, Vladimir Grishchenko wrote:

> Date: Sun, 11 Nov 2001 18:30:37 -0800
> From: Vladimir Grishchenko <vl...@hotmail.com>
> Reply-To: Tomcat Users List <to...@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:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>