Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

setAttribute() for session object and setAttribute() for request object??  RSS feed

 
darine darine
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
What is the difference of using setAttribute for session object and setAttribute for request ServletRequest ??
when do we use this over using that and in which cases?
thanks alot.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by darine darine:
What is the difference of using setAttribute for session object and setAttribute for request ServletRequest ??
when do we use this over using that and in which cases?

The difference is the scope, i.e., the lifetime of your attribute. Session-scoped attributes remain visible as long as your client's session remains valid; you would use it to keep track of conversational state (user information, shopping cart contents, whatever).
Request-scoped attributes on the other hand remain valid only for the duration of the request. This is especially useful when a single request is handled by more than one servlet or JSP. For instance, you could have a controller servlet which creates a JavaBean with information which needs to be displayed (say, product information), binds it as a request attribute, then forwards the request to a JSP. The JSP then accesses the information from the JavaBean to generate the HTML. Once that has been done, the servlet engine discards the request object and, with it, the JavaBean.
In my experience, session-scoped objects are often used where request-scoped objects would be more appropriate. On one hand, this is a pity, because session-scoped objects consume valuable memory which scales with server load. Also, they formally need to be threadsafe (is there anyone who does this?) On the other hand, request-scoped objects have to be recreated for every request, increasing your application's object churn.
- Peter
 
darine darine
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your explanation,but still unclear for me the duration for the request?i know the session object lives for the session period,but about the duration of the request,sorry i'm not clear about it ,can you please explain more.
thanks
 
Maky Chopra
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assume you are collecting information about a person across a series of jsp pages.. name.jsp -> address.jsp ->age.jsp
When someone fills in the name and submits to address.jsp, address.jsp will have req.getParamter("name") in its scope. Then the person filles in the address and submits to age.jsp
If age.jsp does a req.getParameter("name") it will get null since it has gone out of scope. To collect information in this manner you would have to have the name as a hidden field in address.jsp
But if you had put name in a session("name") it would have been available to age.jsp and further on to all the pages accessed during that session
Hope this helps.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!