This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Life of an attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Life of an attribute" Watch "Life of an attribute" New topic
Author

Life of an attribute

Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Hi i wanted to know about the life of an attribute , suppose we go like
request.setAttribute("Name","Value");
or
servletcontext.setAttriute("Name","Value");
or
session.setAttribute("Name","Value");

Now i wanted to know when will these values remain ..

Consider the following scenario:
First Request:
User Request ->Container->Servlet (The servlet inserts attributes using any of above method)->Send response back to container (which sends it to the user)

Now suppose the user makes a second request repeating the above cycle my question is will the attributes which i had inserted in the first cycle be available to the servlet or are all the attributes of the order and context cleared everytime a fresh request comes in ??

My question is will the att


Don’t look where you fall, but where you slipped
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

All these question can be answered by looking through the Servlet Spec, or trying it yourself.

But in brief, scoped variables place in a request last for the lifetime of the request. Those placed in the session last for the lifetime of the session. And those placed in application context last the lifetime of the application.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Suhas Mandrawadkar
Ranch Hand

Joined: Jul 21, 2007
Posts: 72

request.setAttribute("Name","Value"); <- this will be lost as soon as you are done with your request, that is after sending back the response.
Request attribute will be alive as long as that request is being processed (in your example first request).

session.setAttribute("Name","Value"); <- this will be lost after you are done with your session. Generally spans several requests.

servletcontext.setAttriute("Name","Value"); <- this will be lost when you undeploy your web app.


Regards, Suhas S. Mandrawadkar.
Certifications: SCJP 6, SCWCD 5, Oracle WebLogic Server Administrator, OCE Java EE 6 EJB Developer
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Suhas Mandrawadkar wrote:request.setAttribute("Name","Value"); <- this will be lost as soon as you are done with your request, that is after sending back the response.
Request attribute will be alive as long as that request is being processed (in your example first request).

session.setAttribute("Name","Value"); <- this will be lost after you are done with your session. Generally spans several requests.

servletcontext.setAttriute("Name","Value"); <- this will be lost when you undeploy your web app.



Thanks for the great explination... couldnt expect a better one...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Life of an attribute
 
Similar Threads
Chapter 2 (HFSJ) notes , may be useful for anyone
Servlet Container related query
doubt in page 356 of HFSJ
error handling servlet?
Doubt regarding the cookies.