Looking at the JavaDoc for the listeners will tell you what events are "listened for" and when the implementing methods are called.
Joined: Apr 29, 2009
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 ?
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.
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.
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.