• Post Reply Bookmark Topic Watch Topic
  • New Topic

why request and context attributes are not removed when out of scope?

 
kamalakannan kamal
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a doubt regarding scope of attributes.

When a session object goes out of scope, the attributes in it are removed but when request and context object goes out of scope the attributes still remains in it. Can any of you explain me why is it so?
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a session object goes out of scope, the attributes in it are removed but when request and context object goes out of scope the attributes still remains in it. Can any of you explain me why is it so?


When the object itself goes out of scope, how can you say that it contains the attributes still remain with it?
When the request and session object goes out of scope both are garbage collected.
The context object remains in scope for the entire web application and will go out of scope only when the application is shut down.


Hope this helps
 
kamalakannan kamal
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Amit Ghorpade for your reply.

I wrote attribute listener class for all three scopes i.e., ServletRequestAttributeListener, ServletContextAttributeListener, HttpSessionAttributeListener and included three methods i.e., attributeAdded(), attributeReplaced(), attributeRemoved() in these classes.

I also added attributes for all the scopes request, context and session. When request and context object goes out of scope, attributeRemoved() method (of ServletRequest or ServletContext) is not executed but when session object goes out of scope, HttpSessionAttributeListener.attributeRemoved() method is getting executed.

Can you explain me why this happens?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
1.servletContext is start at application startup time.
servletContext is end at application shutdown or server shutdown time.

so it will available in whole application.so scope of the attribute also live through application.

2.request object is related to the page to which you forward your request. so when ever you call the servlet(in which your setAttribute()stuff embedded) ...then the attributeAdded()will call).
it is like storage area depending on the request(RequestDispatcher)...

Are you able to understand?
 
kamalakannan kamal
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think you didn�t get my question. I know that
1.ServletContext object goes out of scope when application is shutdown,
2.HttpSession object goes out of scope when client didn�t request for a specific time or invalidate() method is called and
3.ServletRequest object goes out of scope after service() method is completed.

I have written ServletRequestAttributeListener, ServletContextAttributeListener and HttpSessionAttributeListener classes and included attributeAdded(), attributeReplaced() and attributeRemoved() methods in all these three listener classes.

My doubt is when HttpSession object goes out of scope(invalidate() method is called), removeAttribute() method in HttpSessionAttributeListener class is called for every attribute attached in the HttpSession object indicating that all attributes are removed. But this does not happen when ServletRequest object(after service() is completed) and ServletContext object(when application is shutdown) goes out of scope.

Can anybody know why this so?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!