*
The moose likes Servlets and the fly likes setAttribute() and getAttribute()!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "setAttribute() and getAttribute()!!!" Watch "setAttribute() and getAttribute()!!!" New topic
Author

setAttribute() and getAttribute()!!!

Brian Smith
Ranch Hand

Joined: Oct 26, 2002
Posts: 232
hi folks,
looking at the ServletAPIs, i found that there are a number of interfaces and objects that has getAttribute() and setAttribute() methods. For example, request object, session object and context object all have setAttribute() and getAttribute() methods. with all these, i am confused and can't figure out the exact situation where each one of these is exclusively used? Can setAttribute and getAttribute in session object replace the job done by setAttribute and getAttribute in request object or context for that matter? please clarify these fundamental differences.
thanks.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
The set and get attribute methods are used to attach/retrieve arbitrary objects but the session, request, and context are used for different purposes, have different lifetimes, and have different scopes that they are "visible" in. They compliment each other rather than serve as replacments.
request: has a lifetime of a single request/response cycle. You would attach an attribute when forwarding or includeing with RequestDispatcher
session: has a lifetime of a single user's session (multiple requests with the same browser), can only be seen by this user.
context: has a lifetime of the servlet object - can be seen by all servlets in this web application.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setAttribute() and getAttribute()!!!
 
Similar Threads
what is the attribute in servlet ?
Difference between getAttribute and getParameter
How to display previous values?
passing object from one servlet to the other.
Doubt in setAttribute() for Request object