aspose file tools*
The moose likes Servlets and the fly likes Real Role of Listeners in actual application? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Real Role of Listeners in actual application?" Watch "Real Role of Listeners in actual application?" New topic
Author

Real Role of Listeners in actual application?

muksh sharma
Ranch Hand

Joined: Apr 29, 2009
Posts: 37
Hi guys,
I am preparing for the SCWCD and came across with the Listeners.
Can anybody elaborate, How we use it in real web development?
And what is the actual purpose.


I need a very down to real and simple example.

Please help.

[Java FAQs] [SCWCD Links] [SCBCD Links] [SCWCD Wall of Fame] [SCBCD Wall of Fame]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

There are many types of listeners. To which are you referring?

The only one I consistently use is a context listener to perform startup operations.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
muksh sharma
Ranch Hand

Joined: Apr 29, 2009
Posts: 37
Bear Bibeault wrote:There are many types of listeners. To which are you referring?

The only one I consistently use is a context listener to perform startup operations.


Actually I am pointing to three listeners.
Context, Request and Session.

How we use context listener to perform startup operations?
Ans how other two listeners are used ?
I am trying to understand the coding of JForum and want to work on a real project like that.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Looking at the JavaDoc for the listeners will tell you what events are "listened for" and when the implementing methods are called.
muksh sharma
Ranch Hand

Joined: Apr 29, 2009
Posts: 37
Bear Bibeault wrote:Looking at the JavaDoc for the listeners will tell you what events are "listened for" and when the implementing methods are called.

I understood the working for all three Listeners. But when it comes to the real implementation..I am confused.
Do they play any role to synchronize the database.(As I imagine ..they must play).
And If I am right then how ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
I'm not sure what you mean by "synchronize the database", but the listeners cause arbitrary Java code to run. So they can do anything you can imagine doing with Java code.

As Bear said, context listeners are used for startup/shutdown code - for instance, setting up data structures you need during the application's life, or setting up background threads that you want to run periodically.


Ping & DNS - my free Android networking tools app
Kamlesh Kumar
Greenhorn

Joined: Apr 17, 2006
Posts: 8
Mukesh,

A simple real life example of servlet context listner is:

- In the contextInitialized method, read an XML file containing some information which might be required anywhere in the web application.
- Store the information in some data structure, say, map and then store that variable in the application context.
- Now you can use the variable on any of the JSP/Servlet of your web application.
- You can remove the variable from application context in the contextDestroyed method.
- So, the XML will read only once at the server startup time.

Hope this helps.

- Kamlesh
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30771
    
156

muksh sharma wrote:I am trying to understand the coding of JForum and want to work on a real project like that.

JForum uses one session listener. It removes the session from a map when the session is destroyed. Most applications wouldn't store the sessions in a map in the first place. JForum does so that if you log in from another computer before your session has expired, it can properly display the icons for which threads are new.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
muksh sharma
Ranch Hand

Joined: Apr 29, 2009
Posts: 37
thanks Bear Bibeault , Ulf Dittmer, Kamlesh Kumar, Jeanne Boyarsky.

I did.t expect such a reputated person's reply for my Query.

I will resume this thread again but I have to prepare myself to ask a good question.

-Muksh

 
Don't get me started about those stupid light bulbs.
 
subject: Real Role of Listeners in actual application?