File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Listeners doubt?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Listeners doubt??" Watch "Listeners doubt??" New topic
Author

Listeners doubt??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9383
    
    2

Ranchers,

I'm now reading the chapter on Listeners for the 2nd time and still I'm not able to get what exactly are Listeners for?? Well the definition says that "Application event listeners are classes that implement one or more of the servlet event listener interfaces." What exectly they come to say??Anyone on this please.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
Listeners are mainly used in swings.
In event handling there are mainly two terms which needs attention
1.Listener
2.Event
For every Listener there is event associated with it.
For example in swing when we press a button a event is generated which is handled by Listener.
you can think event as a message which is generated when specific action is performed and this message is handeled by a listener.
So when web application is loaded a event known as ServletContextEvent is generated which is handled by ServletContextListener.
Same way HttpSessionEvent is generated when a session is created and this event is handled by HttpSessionEvent.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9383
    
    2

Thanks Gowher. Why then we use Listeners?? We have attributes, parameters...why do we need Listeners explicitly??
Renu Radhika
Ranch Hand

Joined: Oct 21, 2005
Posts: 243
Listeners are needed for taking specific actions when certain types of event take place which cant be done with attributes
Micheal John
Ranch Hand

Joined: Nov 01, 2006
Posts: 344
Suppose you have session and want to add an attribute to the session and before adding to session, if you want to do any work with an attribute, then you want listener (HttpSessionAttributeListener, which has three methods to be override attributeAdded(HttpSessionBindingEvent e),attributeRemoved(HttpSessionBindingEvent e),attributeReplaced(HttpSessionBindingEvent e)). Your code should go inside the attributeAdded(HttpSessionBindingEvent e)..

Like session to check any attribute are added/removed/replaced, you have for request and context (ServletRequestAttributeListener, ServletContextAttributeListener)

You have many listeners, but the basic idea is to do something when the desired event is triggered, which will be notified automatically by listener.

Since you have HFSJ, they given the example for ServletContext and HttpSession listerners..

May be this helps you..


Micheal John
SCJP 1.4 (86%), SCWCD 1.4 (86%), SCBCD 1.3 (85%), SCDJWS (Just Started...) - Satisfaction Lies in Our EFFORT, Not in the ATTAINMENT
Dean Jones
Greenhorn

Joined: Dec 13, 2006
Posts: 23
I still don't get it.

If you know when you add an attribute and you know you need to use the attribute that you added, why not just do the work then? Why do you need a listener?

Let's say someone submits a form for a shopping cart, you have a doPost method and you handle them adding an item to their shopping cart (storing the stuff in the database or session data, whatever...) Then they move on to shopping and add more items, etc... Then they go to checkout. You have a servlet to handle checking the person out too... The checkout pull the session data, charges them checks them out... we're done.

At what point would there need to be a Listener to help with that application? Why does a class need to be sitting around waiting for another class to add a parameter or attribute??
Dean Jones
Greenhorn

Joined: Dec 13, 2006
Posts: 23
how come threads die when I post a question? hehe
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9383
    
    2

Dean,

Even I'm still not clear and looking for some more explanations. Michael's explanation was fairly good but I would be happy if I can get a more detailed explanation.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41839
    
  63
The concept is knows as "application lifecycle events", and there is a chapter on it in the servlet spec (e.g., chapter 10 in the Servlet 2.4 spec). That would be a good starting point for some research.

The point is basically separation of concerns. Event listeners are independent of any servlet code, and can be reused/triggered by events happening in multiple places. The developer doesn't need to worry about remembering to put the code in all the places where an event might be triggered - he just puts it into the listener once, and is done.

The analogy to GUI listeners would be that you don't want to check -every time the mouse was clicked- whether the click happened inside the area covered by a button, and then do whatever needs to happen upon a button click. You just want to specify what should happen if a button is clicked (in a listener), and then let the JVM worry about invoking the listener at appropriate times.


Ping & DNS - my free Android networking tools app
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Listeners doubt??