aspose file tools*
The moose likes Servlets and the fly likes session.setAttribute and request.setAttribute, what's the diff? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "session.setAttribute and request.setAttribute, what Watch "session.setAttribute and request.setAttribute, what New topic
Author

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

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
Hi,
what are the differences between:

request.setAttribute("A", objA);
and
HttpSession session = request.getSession();
session.setAttribute("B", objB);
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Sep 10, 2004
Posts: 755
* Perfect * thanks!!!
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Wow, it's nice to see Australian politicians visiting our forums and learning Java.


GenRocket - Experts at Building Test Data
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: session.setAttribute and request.setAttribute, what's the diff?