• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic