I am learning servlets in Head First Servlets and JSP. This book is good for the beginners. I have to appreciate the authors of this book for their way of presentation. I also learned java in head First Java.
Now the problem I am facing: I know what is ServletConfig and ServletContext. I am not clear with ServletContextListener. Why there are using it. Please anyone explain it. It seems to understandable, but somewhat in some section it is not clear.
An Example of ServletContextListener Use: To illustrate a use of the event scheme, consider a simple web application containing a number of servlets that make use of a database. The developer has provided a servlet context listener class for management of the database connection. 1. When the application starts up, the listener class is notified. The application logs on to the database, and stores the connection in the servlet context. 2. Servlets in the application access the connection as needed during activity in the web application. 3. When the web server is shut down, or the application is removed from the web server, the listener class is notified and the database connection is closed.
ServletContext created when application deployed or server started ServletContext destroyed when for example server was shut down
can anybody explain this concept of ServletContextListener by the easiest example
Niyas Ahmed Sheikh
Joined: Jun 15, 2005
To illustrate a use of the event scheme, consider a simple web application containing a number of servlets that make use of a database. The developer has provided a servlet context listener class for management of the database connection. 1. When the application starts up, the listener class is notified. The application logs on to the database, and stores the connection in the servlet context.
Thanks for your reply Akbar.
So, your saying if we are having a number of servlets that make use of database, we are using ServletContextListener.
Correct me If I am wrong in the below one: ServletConfig is for one per servlet. ServletContext is for one per web appln. Why can't we use the database connection in the ServletContext, since it will be one per the web application(so all the servlets and JSP can use it). What is speical about ServletContextListener?
As far my understand or in the book, they are stated that ServletContextListener is used to change(convert?) the init param(Context) in the web.xml(DD) to an object. This is where I struck with.
as you know, the ServletContextListener listens to servlet context events(application events)
say, you want to log to a database when the application is 'upped' or when it is 'downed'. you simply create a class that extends the listener, and write the code in teh appropriate method, and define this class in teh web xml(or TLD), and there you go.
Joined: Feb 26, 2005
heres a funny example i just thought of:
you are production support for web application pointed to by www.jesusangeles.com. it had some problems so you shut down the web application, while you debug it. but you gotta sleep.
so you tell the sys ad, 'whatever you do, dont start the application, i am still fixing it'
but you dont trust him, so you use ServletContextListener so that when the application is started, it will send your cellphone a message 'app is upped'.
and so you slept, and it happened. you called the sys ad and told him to shut it down.
you also made the ServletContextListener to send you a message when the application is shut down, and you will get a message 'app is downed'
then you get that 'app is downed' message, and tell the sys ad, 'please dont do that again, i am trying to get some sleep here'
I would like to clear your doubt with a few simple points. 1)you assume that you want you instantiate an object before any of your servlets are loaded in the web-app at container startup. ------->remember!!! you want an object to be instantiated ------->and you cant keep object reference variables in DD. 2)now you can think that "if i need to instantiate an object, i can keep it in my servlet and it takes care of the object instantiation". ------->But is that servlet going to be loaded first at container startup. ------->you cant assure that it is going to be loaded first. ------->and only when it is loaded first the object can be instantiated before other servlets can use it. 3)so dont think about keeping that code in the servlet. 4)so u need something that instantiates an object before any servlet 5)here comes the listener. 6)now the ServletContextListener is the one which triggers the following things. ---->it asks the container to give a reference to the ServletContextEvent(ServletContext Object creation) ---->now from the ContextEvent Object it gets the Context Params(you keep the object name as context param) ---->now it instantiates the Object using that name and stores it at the ServletContext