You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Erin Lester <ec...@student.math.uwaterloo.ca> on 2001/07/19 22:36:38 UTC

How to forward with new request/query string?

I was wondering how you go about transferring control from a servlet to
another page (in this case a jsp page) before committing the response.  I
need to send request variables to this new page.  I've tried using a
request dispatcher, but I believe that this just sends the request that
the servlet got and not a new one with the querystring values (which are 
appended to the url I want to forward to) in it.

Can someone tell me how to do this?  Also, what's the difference between
sendRedirects, dispatcher forwards, etc. ?  and is there any way to
transfer control to another page and have the user's location bar reflect
the address of the new page?

Any help would be appreciated!
- Erin


Re: How to forward with new request/query string?

Posted by Jeff Kilbride <je...@kilbride.com>.
Hi Erin,

You can't change the original request (at least, I don't think you
can...) -- but you can add info to the request before forwarding it on to
your jsp page. Just add the extra info to the query string of the jsp your
forwarding to:

String location = "myForward.jsp?foo=bar&baz=1";
RequestDispatcher rd = request.getRequestDispatcher(location);
rd.forward(request, response);

The parameters foo and baz will now be available in myForward.jsp, along
with all the original request params. If foo and baz exist in the original
request, calling request.getParameter() will return the newly set value and
calling request.getParameterValues() will return all values for that
parameter.

You can also store info in the request object using setAttribute() and
retrieve them in the jsp by using getAttribute():

(in servlet)
request.setAttribute("key", value);

(in jsp -- if the value is a String object)
String value = (String)request.getAttribute("key");

Notice that you have to cast the object back to the correct type in your
jsp.

Hope this helps.

--jeff

----- Original Message -----
From: "Erin Lester" <ec...@student.math.uwaterloo.ca>
To: <to...@jakarta.apache.org>
Sent: Thursday, July 19, 2001 1:36 PM
Subject: How to forward with new request/query string?


> I was wondering how you go about transferring control from a servlet to
> another page (in this case a jsp page) before committing the response.  I
> need to send request variables to this new page.  I've tried using a
> request dispatcher, but I believe that this just sends the request that
> the servlet got and not a new one with the querystring values (which are
> appended to the url I want to forward to) in it.
>
> Can someone tell me how to do this?  Also, what's the difference between
> sendRedirects, dispatcher forwards, etc. ?  and is there any way to
> transfer control to another page and have the user's location bar reflect
> the address of the new page?
>
> Any help would be appreciated!
> - Erin
>