• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Listeners doubt

 
Sai Surya
Ranch Hand
Posts: 463
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

I would like to clarify few of the following doubts.

1. When implementing ServletContextListener, contextInitialized() called after the context is initialized and
contextDestroyed() called before the context is being destoryed to do clean up etc. Am I correct?

2. Is the same rule applies to other listener events like sessionDidActivate(), sessionWillPassivate(), requestInitialized(), requestDestroyed(), sessionCreated(), sessionDestroyed()? I mean, creation event is called when 'certain thing' is initialized and ready. And destory event called before 'certain thing' goes down to allow to prepare. Am I correct?

3. In the case of events like valueBound(), valueUnBound(), attributedAdded(), attributeRemoved(), attributeReplaced(),
event will be triggered before everything is done like adding, replacing or removing attribute.

I've done code examples and realized that my assumptions are correct . Someone please validate my assumptions.

Just to double confim
 
Chinmaya Chowdary
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sai.
1. When implementing ServletContextListener, contextInitialized() called after the context is initialized and
contextDestroyed() called before the context is being destoryed to do clean up etc. Am I correct?

2. Is the same rule applies to other listener events like sessionDidActivate(), sessionWillPassivate(), requestInitialized(), requestDestroyed(), sessionCreated(), sessionDestroyed()? I mean, creation event is called when 'certain thing' is initialized and ready. And destory event called before 'certain thing' goes down to allow to prepare. Am I correct?

I think correct.

3. In the case of events like valueBound(), valueUnBound(), attributedAdded(), attributeRemoved(), attributeReplaced(),
event will be triggered before everything is done like adding, replacing or removing attribute.


I think event will be triggered 'after' everything is done.
 
Sai Surya
Ranch Hand
Posts: 463
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3. In the case of events like valueBound(), valueUnBound(), attributedAdded(), attributeRemoved(), attributeReplaced(),
event will be triggered before everything is done like adding, replacing or removing attribute.


I think event will be triggered 'after' everything is done.

I tried the following and got the output.

In servlet:



In listener:


Output:

Setting request attribute in servlet begin
**** MyServletRequestAttributeListener.attributeAdded() begin
**** MyServletRequestAttributeListener.attributeAdded() end
Setting request attribute in servlet end

That means, before setting the attribute the event is triggered. Confused on this

I observed the same behaviour for other listeners as well.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




What makes you think the event is triggered before setting the attribute..

Go step wise.

First line of the servlet runs and you get the output :
Setting request attribute in servlet begin

Then you add the attribute. ie . request.setAttribute("yourDog", dog);
This triggers the attributeAdded() method.

So your next output is :
System.out.println("**** MyServletRequestAttributeListener.attributeAdded() begin");
System.out.println("**** MyServletRequestAttributeListener.attributeAdded() end");


And finally you go back to the servlet code.
So the final out out is :
System.out.println("Setting request attribute in servlet end");


So the whole output is :
Setting request attribute in servlet begin
**** MyServletRequestAttributeListener.attributeAdded() begin
**** MyServletRequestAttributeListener.attributeAdded() end
Setting request attribute in servlet end


Just as you got...
So.. what seems to be the problem !?

 
Sai Surya
Ranch Hand
Posts: 463
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes! Event will be triggered after setting/modifying/removing attribute. I put the folloinwg in each event and realized this.

Thanks Nabila
 
Chinmaya Chowdary
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Surya.
In the following code

after has executed, then ServletRequestListener's will be executed. It means after the attribute was set in request scope, event was triggered.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic