• 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

session.setAttribute and request.setAttribute, what's the diff?

 
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
what are the differences between:

request.setAttribute("A", objA);
and
HttpSession session = request.getSession();
session.setAttribute("B", objB);
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The difference is called the "scope". If you put a variable in a request, it will disappear when the request is over. If you put a variable in a user session, it will disappear when the session is over.

When do you use one, rather than the other ? It depends on what you want to do. You may need a variable only in a jsp file, so you would put it in the request. You may also need to keep the user information, where it can be accessed anywhere, so you would put it in the session.
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got you!
Q:
I. are there any other scopes?
II. can you give an example where you'll use request.setAttribute("A", objA);

III. What if I wish to set an object AA to all users? as appose to unique users HttpSession session = request.setAttribute("UserInfo", userInfo)


thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I. are there any other scopes?


Application scope. You can use ServletContext to set attributes, which will be available for al servlets.

II. can you give an example where you'll use request.setAttribute("A", objA);


As I said in the first post, when you want to pass a variable needed by a jsp file.
For example, a list of employees. You could put the list in the request, forward to a jsp, and display the list.

III. What if I wish to set an object AA to all users?


Same as question I
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
* Perfect * thanks!!!
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, it's nice to see Australian politicians visiting our forums and learning Java.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wow, it's nice to see Australian politicians visiting our forums



 
reply
    Bookmark Topic Watch Topic
  • New Topic