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.
Author and all-around good cowpoke
posted 12 years ago
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