Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setAttribute() and getAttribute()!!!

 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic