• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Listener Invoking Sequence

 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the precedence relationships w.r.t listeners?

Fot example I saw in "David Bridgewater"s mock test that if a web application implements interfaces HttpSessionBindingListener and HttpSessionAttrbite listener then ...

When a variable is added valueBound() is called prior to attributeAdded(). Is this a rule? If yes, what are the other rules?
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Listeners will be called in order as it appears in DD. When apps shutdown it will be called in the reverse order.
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, is output predictable for the below question?

===
A web application houses an HttpSessionAttributeListener and an object (SessionAttrObject) that implements HttpSessionBindingListener. Pick out the correct sequence of listener method calls that follows from executing 1 the servlet code below inside this web application. (Choose one.)
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Question20 extends HttpServlet {
protected void doGet (HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.invalidate();
HttpSession newSession = request.getSession();
SessionAttrObject boundObject = new SessionAttrObject ("value");
newSession.setAttribute("name", boundObject);
newSession.setAttribute("name", "value");
newSession.setAttribute("name", null);
}
}

A.attributeAdded(), valueBound(), attributeRemoved(), valueUnbound(), attributeReplaced(), attributeRemoved()
B.valueBound(), attributeAdded(), valueUnbound(), attributeRemoved(), attributeReplaced(), attributeRemoved()
C.valueBound(), attributeAdded(), valueUnbound(), attributeReplaced(), attributeRemoved()
D.attributeAdded(), valueBound(), attributeReplaced(), valueUnbound(), attributeReplaced()
E.valueBound(), attributeAdded(), valueUnbound(), attributeRemoved(), valueBound(), attributeAdded(), valueUnbound(), attributeRemoved()
F.None of the above.

===
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it will be called in the reverse order..

and sessions listeners are notified before attribute listeners.
 
Arvind Giri
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is the extract from java docs of HttpSession.

Hope it would help you in finding right sequence....


removeAttribute(String)
public void removeAttribute(java.lang.String name)

Removes the object bound with the specified name from this session. If the
session does not have an object bound with the specified name, this method
does nothing.
After this method executes, and if the object implements HttpSession-
BindingListener, the container calls HttpSessionBinding-
Listener.valueUnbound. The container then notifies any
HttpSessionAttributeListeners in the web application.

Parameters:
name - the name of the object to remove from this session

Throws:
IllegalStateException - if this method is called on an invalidated session
 
Arvind Giri
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry guys, Instead of posting jova docs of setAttribute method I posted java doc of removeAttribute.

Here is the javajoc for setAttribute method:

setAttribute(String, Object)

public void setAttribute(java.lang.String name,
java.lang.Object value)

Binds an object to this session, using the name specified. If an object of the same name is already bound to the session, the object is replaced.
After this method executes, and if the new object implements HttpSession-
BindingListener, the container calls HttpSessionBinding-Listener.valueBound. The container then notifies any HttpSessionAttributeListeners in the web application.


If an object was already bound to this session of this name that implements
HttpSessionBindingListener, its HttpSessionBindingListener.value-
Unbound method is called.


If the value passed in is null, this has the same effect as calling remove-
Attribute().

Parameters:
name - the name to which the object is bound; cannot be null
value - the object to be bound

Throws:
IllegalStateException - if this method is called on an invalidated session

 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manikandan,

Yes it is preditable. And answer is

C. valueBound(), attributeAdded(), valueUnbound(), attributeReplaced(), attributeRemoved()
 
Celinio Fernandes
Ranch Hand
Posts: 549
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prabhu venkatachalam:
Manikandan,

Yes it is preditable. And answer is

C. valueBound(), attributeAdded(), valueUnbound(), attributeReplaced(), attributeRemoved()


Can you explain that answer ?
I do not understand the second "valueUnbound()".
I guess it happens with this line :
" newSession.setAttribute("name", boundObject);"
but that does not unbound the value "boundObject" from the newSession (or does it ??) so i am confused.

Or does it mean an attribute is considered unbound from a session from the moment it is added into the session ?

OK I think I got it :
" newSession.setAttribute("name", "value" );" unbounds boundObject from the session.
[ November 21, 2006: Message edited by: Max Fernandes ]
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I think I got it :
" newSession.setAttribute("name", "value" );" unbounds boundObject from the session.


yes, you are right.
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Prabhu and Arvind!

So in crux,
Binding Listeners get invoked before attribute Listeners.

Also,

ServletContextAttributeListener's attributeRemoved() gets invoked after the ServletContextListener's contextDestroyed() method. This is because contextDestroyed() removes the attributes from the context. Am I right?

If I am right, then listeners dont get triggered based on DD order of declaration. Am I right?
=====
Niranjan said ...
"Listeners get invoked and removed in the order and reverse-order of declaration in DD respectively"

Does this talk only about listener's instantiation OR even the trigerring of the listeners ?

Can someone justify?
 
Arvind Giri
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Niranjan is right!

If there are more than one HttpSessionAttributeListener then they are invoked in the order, they are declared in DD.

According to servlet specs:
SRV.10.3.2 Deployment Declarations
Listener classes are declared in the Web application deployment descriptor using the listener element. They are listed by class name in the order in which they are to be invoked.


To summarize, Binding Listener gets invoked before attribute Listeners and attribute listeners are invoked according to the order of their declaration in DD(If there are more than one attribute listener).
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Arvind! I am clear with all the justifications. Just want some further information ...

The order of declaration in DD, determines the order of invocation between the classes that implement same kind of listeners.

But there do exist a precedence rule between different listeners. Leave the obvious precedence rules. Are their some well defined precedence rules apart from Binding Listener gets invoked before Attribute Listener.
 
Arvind Giri
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes! There are certain other rules too.

I hope you will find this link useful http://www.coderanch.com/t/173384/java-Web-Component-SCWCD/certification/order-listeners

Its regarding invocation order of listener when application server shuts down.
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thnx Arvind!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic