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.
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 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 ?
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.
Hope this helps.
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.
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.