Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
what are the differences between:

request.setAttribute("A", objA);
and
HttpSession session = request.getSession();
session.setAttribute("B", objB);
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
* Perfect * thanks!!!
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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 Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, it's nice to see Australian politicians visiting our forums


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic