File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Real Role of Listeners in actual application?

 
muksh sharma
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
muksh sharma
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kamlesh Kumar
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33694
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
muksh sharma
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic