There are many times of Listeners. Now I will explain about
ServletContextListener.
When you want to do some thing when ServletContext is being created
you have to write a class that implements ServletContextListener interface.
Eg:If I want to create a student object and share it across all jsp/servlets
I will write a class that implements ServletContextListener interface,one
of the method is contextInitalized.. In that method.. I will create student
object and set it in ServletContext object. So by the time my application
is started.. student object can be read by any servlet or jsp.
2)If you want to create datasource and share it across all jsp/servlets
Please refer ContextListener.java and listenerTester.java
in HelloWorldWithListener.zip that i have uploaded.
In ContextListener.java I have set javalearners arraylists in
Servletcontext.(Here you can create datasource or what ever object you
like to make it available to all servlets or jsp pages) and reading that
list in listenerTester.You can write a new servlet or
jsp page and try to read that arraylist. That arraylist is accessible by
all the jsp pages and Servlets.
Hope you understood listener concept.
No comments:
Post a Comment