• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing Parameters

 
Naveed Ali
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Naveed Ali
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic