• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Life of an attribute

 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Suhas Mandrawadkar
Ranch Hand
Posts: 72
Android Java Oracle
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic