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