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 Web Component Certification (SCWCD/OCPJWCD) and the fly likes Order of Listeners 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 » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Order of Listeners" Watch "Order of Listeners" New topic
Author

Order of Listeners

Srividhya Kiran
Ranch Hand

Joined: Feb 17, 2008
Posts: 166
Hello ranchers

Consider this code:

<listener>
<listener-class>MySessionListener</listener-class>
</listener>
<listener>
<listener-class>MyContextListener</listener-class>
</listener>

The order in which its getting called is first ContextListener then SessionListener. But I read in one of the mock exam that listeners are called in the order they are declared in the DD i.e, first SessionListener then ContextListener. Please let me know which one is correct.

Thanks
Srividhya
Paulo Rezende
Ranch Hand

Joined: May 13, 2008
Posts: 33

If you have two (or more) listeners listening to the same thing, the order in the DD cares, but in your example you have two listeners listening different things.

MySessionListener is listening sessions initializations and destructions.

MyContextListener is listening context initialization and destruction (they happens 'almost' once, they can happen twice if you restart the server or the app).

Of course, a context initialization will happen before any session initialization, and the sessions destructions will happen before the context destruction.

But, I really don't if the container call the session destructions events before shutdown. It is really a very good question?


SCJP, SCWCD, OCBCD
Srividhya Kiran
Ranch Hand

Joined: Feb 17, 2008
Posts: 166
Paul

I think the container calls the session destruction before the context destroyed. I have checked that. Anyway thanks for your reply.

Srividhya
Bala'J'i Rags
Greenhorn

Joined: Jul 29, 2008
Posts: 26
Hi srividya,

So does it mean that the container executes in the order given in the DD?


SCJP 1.5,SCWCD 1.5
Bala'J'i
Paulo Rezende
Ranch Hand

Joined: May 13, 2008
Posts: 33

The container will always destroy the session before destroy the context.

Even you declare in the DD a listener for context before a listener for session.

Then, in this case, the session's listener will be called before the context's listener.

The order cares if you declare many listeners that extends ServletContextListener and many listeners that extends HttpSessionListener.

The container will execute before the session's listeners, in the order of DD declaration.

Then container will execute the context's listeners, in the order of DD declaration.
[ August 06, 2008: Message edited by: Paulo Rezende ]
Paulo Rezende
Ranch Hand

Joined: May 13, 2008
Posts: 33

Remember, the listerners just listen

They don't change the order which the container works.
[ August 06, 2008: Message edited by: Paulo Rezende ]
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
and first the contextlistener(construction) will be called and then sessionlistener(construction) and then again sessionlistener(destruction) and then contextlistener(destruction)


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Order of Listeners
 
Similar Threads
Multiple ServletContextListner event handling order
order of listeners
Regarding order of listener elements
about listener
ContextLoader No log appenders found in Tomcat