Meaningless Drivel is fun!
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "setAttribute() and getAttribute()!!!" Watch "setAttribute() and getAttribute()!!!" New topic

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.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
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.
I agree. Here's the link:
subject: setAttribute() and getAttribute()!!!
jQuery in Action, 3rd edition