• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Listeners doubt??

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Gowher. Why then we use Listeners?? We have attributes, parameters...why do we need Listeners explicitly??
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Listeners are needed for taking specific actions when certain types of event take place which cant be done with attributes
 
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how come threads die when I post a question? hehe
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic