my dog learned polymorphism*
The moose likes JSP and the fly likes Passing Parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Passing Parameters" Watch "Passing Parameters" New topic
Author

Passing Parameters

Naveed Ali
Ranch Hand

Joined: Aug 30, 2002
Posts: 41
Hi all,

Can we pass variables declared in one jsp as parameters in request(through 'jsp forward' or 'sendredirect') to another jsp. Actually in my case i have an array list/or it may be 'map', that i want to pass to another jsp through 'jsp forward'. Also i need to know that can i store them in a session so that i could get it in another jsp.? Thanks in Advance.

Naveed
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Yes, yes, and yes.
Look at the getAttribute and setAttribute methods.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpSession.html

These methods also exist (and work the same way) in the request and context objects.
You will, of course, need to pick the correct scope for what you are doing.
IE: You will not be able to pass objects using request scope if you are using sendRedirect because you will be working with a new request in the next JSP.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Naveed Ali
Ranch Hand

Joined: Aug 30, 2002
Posts: 41
Ben,

IE: You will not be able to pass objects using request scope if you are using sendRedirect because you will be working with a new request in the next JSP.

Don't understand why can't we pass objects using request scope. Can we do
it using jsp:forward? If yes, what is the difference ?

Naveed
[ September 09, 2005: Message edited by: Naveed Ali ]
Sripathi Krishnamurthy
Ranch Hand

Joined: Mar 07, 2005
Posts: 232
Originally posted by Naveed Ali:
Don't understand why can't we pass objects using request scope. Can we do
it using jsp:forward? If yes, what is the difference ?


If you pass the same request object to the new page, then the new page can access the objects passed along with that request.
If the new page uses a new request object, then the previous objects stored are lost.
jsp:forward forwards the same(current) request. hence the objects will be available in the new page also.
Prakash Balasubramani
Greenhorn

Joined: Sep 09, 2005
Posts: 8
Hi,

To have an object available in new page while redirecting, u can use redirect to new page as follows

response.sendRedirect(response.encodeRedirectURL(pagename))

This will send the request to new page with the same session-id so all the objects will be available in new pages too.

Hope this could be useful.

Bye,

B.Prakash.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Prakash Balasubramani:
Hi,

To have an object available in new page while redirecting, u can use redirect to new page as follows

response.sendRedirect(response.encodeRedirectURL(pagename))

This will send the request to new page with the same session-id so all the objects will be available in new pages too.

Hope this could be useful.

Bye,

B.Prakash.


No it won't.
Any time you use response.sendRedirect, you are mearly sending a 302 respone code to the browser along with a URL. The browser then makes a new request to the server. Anything bound to request scope is available for that request only.

When using forward, control goes straight to the new page without leaving the server (within the same request).

Prakash,
Please read the API for encodeRedirectURL and understand what it does before telling people what they can do with it.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequest.html
 
wood burning stoves
 
subject: Passing Parameters