File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Listener" Watch "Listener" New topic


sandeep Talari
Ranch Hand

Joined: Dec 24, 2007
Posts: 63
can anybody explain me how to work with ServletContextListener

what is the main purpose of the listener.
I didn't get what head first explain.

give explaination somebody plzzzzzzz
Laukik N. Desai

Joined: Aug 03, 2007
Posts: 8
It is simply used to get the notifications of the context events.
Like,when you want to perform some specific task when context is destroying then you will have to implement this ServletContextListener...
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Originally posted by sandeep Talari:
give explaination somebody plzzzzzzz

Welcome to JavaRanch!

In an effort to help you get the most from our forums, we've compiled a
list of tips for asking questions here. You can find the list in our
FAQ section here.
In particular please see:

Abbreviations such as "u" or "ur" in
place of "you" and "you are" or "you're" confound language translation software making
it hard for our non-English speaking members to read your posts.
"plzzzz" is not a word in the English language.

Again, welcome to JavaRanch and good luck with your question.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

A context listener give you a reliable place to put code that you want executed when a context starts up and shuts down.

Prior to contextListners, the only place where you could put startup and shutdown code was in the init or destroy method of a servlet. This was less than optimal because, first and foremost, servlets were not meant to serve this purpose. They were meant to respond to requests. Secondly, when init code is put in a servlet, it is up to the developers and administrators to make sure that no other servlets, that rely on that startup code, are initialized before that servlet. The load-on-startup attribute is configurable in web.xml so it's possible that an administrator who doesn't know about the init code could alter the order. Thirdly, the spec allows containers to load an unload servlets at any point in the application to conserve resources. If this happens to the servlet containing the init and destroy code, it could cause problems with the application.

Context listener's contextInitialized methods, on the other hand, are guaranteed to be run before any servlets are loaded. Likewise the contextDestroyed method is run only after all of the servlets have been unloaded. It also guaranteed that each of these methods will only be run once in an application's lifecyle. Most importantly, using context listeners allows servlets to do what they were intended to do and nothing else; response to requests.
I agree. Here's the link:
subject: Listener
jQuery in Action, 3rd edition