I want to confirm the differences between request.setAttribute(key, value) versus session.setAttribute(key, value).
My understanding is that request.setAttribute only make the key available in the following jsp page. But session.setAttribute will make the key available in many pages, as long as in the same session, and browser not close.
For example, the flow like page1->page2->page3->page4. session.setAttribute will make the key available in all pages. But if we use request.setAttribute in page2, then only page3 can get the key value set in page2.
Please advise if i miss something.
thanks!!
Discussions
Web tier: servlets, JSP, Web frameworks: request.setAttribute versus session.setAttribute
-
request.setAttribute versus session.setAttribute (5 messages)
- Posted by: Matt Louden
- Posted on: October 26 2004 20:55 EDT
Threaded Messages (5)
- request.setAttribute versus session.setAttribute by Amit Sharma on October 27 2004 01:21 EDT
- using request.setAttribute instead of Hidden fields by Vijay Deshmukh on August 02 2006 10:11 EDT
- request.setAttribute versus session.setAttribute by Nathan Comstock on October 31 2004 23:33 EST
- request.setAttribute versus session.setAttribute by Dennis Cheung on November 02 2004 01:16 EST
- request.setAttribute versus session.setAttribute by Michael Jouravlev on November 18 2004 17:06 EST
-
request.setAttribute versus session.setAttribute[ Go to top ]
- Posted by: Amit Sharma
- Posted on: October 27 2004 01:21 EDT
- in response to Matt Louden
hi Matt,
Your understanding of the difference between request.setAttribute and session.setAttribute is perfect....
But just to add to that
request.setAttribute():
>>>>>May help you in getting rid of the hidden fields.
>>>>>Use it most of times when you just need the attribute just for the consecutive page.
session.setAttribute():
>>>>>May help you in keeping the information through out the session e.g. db connections
>>>>>but be clear where to use it.
Best of Luck..... -
using request.setAttribute instead of Hidden fields[ Go to top ]
- Posted by: Vijay Deshmukh
- Posted on: August 02 2006 10:11 EDT
- in response to Amit Sharma
Dear Amit, Is it possible to use request.setAttribute on a JSP page and then on HTML Submit get the same request attribute in the Servlet? I am just curious how we can do away with the hidden fields, if all we want is to just pass the contents of a huge List containing different objects, without using session. regards Vijay -
request.setAttribute versus session.setAttribute[ Go to top ]
- Posted by: Nathan Comstock
- Posted on: October 31 2004 23:33 EST
- in response to Matt Louden
My understanding is that request.setAttribute only make the >>key available in the following jsp page.
You should also consider how you move from page to page, if you use forward, then you are passing the request and could pass the request along to as many pages as you like. The key will be available as long as the request it was set in is the current request. -
request.setAttribute versus session.setAttribute[ Go to top ]
- Posted by: Dennis Cheung
- Posted on: November 02 2004 01:16 EST
- in response to Matt Louden
Is it very simple to understand.
If you know about the "page scope", you know the "request scope".
request's attirbute is only availible in the "request" object lifetime.
filter, servlet, jsp, include, forward are using same request object.
page's attirbute is only availible in the "page" object lifetime.
You can't use page's attribute in filter, because there is no page loaded yet.
you can use them in the jsp file/servlet, but if you do a forward/include, the attribute in "page" will not pass to them.
>request.setAttribute make the key available in the following jsp page
NO, unless your "following jsp page" is meaning forward the page by <jsp:forward> -
request.setAttribute versus session.setAttribute[ Go to top ]
- Posted by: Michael Jouravlev
- Posted on: November 18 2004 17:06 EST
- in response to Matt Louden
HttpRequest object is created and maintained for each request/response call. Onse response is sent to the client, request object is destroyed. Forwarding from one page to another is done on the server, so the request is preserved. Redirecting is done using roundrip through browser, so old request is destroyed and a new one is created.