This document discusses the application event framework in Java web applications. It describes eight types of listeners that can respond to different life cycle events in a web application, including servlet context, session, and request listeners. These listeners allow developers to monitor and respond to events like the creation and destruction of the servlet context or sessions. The document provides examples of using session listeners to count active sessions and monitor changes to session attributes. It also presents a case study on using a servlet context attribute listener to update an application-wide company name parameter when it changes.
2. What and Why ?
? Life cycle methods
? How to respond to major events in the life cycle of the web application
?
? Who will do what ?
? Eight kind of listeners that respond to web life cycle events.
Prof. AshishSingh Bhatia 2
4. How ?
? Implement the appropriate interface.
? Implement the methods needed to respond to the events of interest.
? Obtain access to the important Web application objects.
? Use these objects.
? Declare the listener.
? Provide any needed initialization parameters.
Prof. AshishSingh Bhatia 4
5. Take a Case Study and Think out of Box
? We are enterprise level [ Huge Company with lot many pages of web
application ]
? Where company whose name changes frequently
? We need to display company name in all pages [ Servlet / JSP ]
? How we can do that ?
Prof. AshishSingh Bhatia 5
6. Take a Case Study and Think out of Box
? As it is needed in application wide we will put it in web.xml as context-param
<context-param>
<param-name>companyName</param-name>
<param-value>test.com</param-value>
</context-param>
? In Servlet we will read it using
String name = getServletContext().getInitParameter(¡°companyName¡±);
Prof. AshishSingh Bhatia 6
Guess we have 100 [ Servlet / JSP ]
If we forgot to add entry in web.xml ?
Every Servlet / Page will display null
!!!!
10. Take a new case
Prof. AshishSingh Bhatia 10
What if servlet change
the companyName of
context-param ?
Example : asbspace.in
11. Take a new case
Prof. AshishSingh Bhatia 11
We should ensure that when company
name is changed
formerCompanyName should also be
changed
companyName = asbspace.in
formerCompanyName=test.com
ServletContextAttributeListener
15. JSP displaying new and old name of the company
Prof. AshishSingh Bhatia 15
16. Session Related Listener
? For all Sessions
? ServletContextListener
? ServletContextAttributeListener
? For Specific Sessions
? HttpSessionListener
? HttpSessionAttributeListener
? Counting number of sessions
? HttpSessionListener Interface : sessionCreated and sessionDestroyed
Prof. AshishSingh Bhatia 16
22. Watching for changes in Session Attribute
? HttpSessionAttributeListener interface. Implement attributeAdded,
attributeReplaced, and attributeRemoved
? It gets notified when an object is placed into the session scope for the
first time, replaced by another object, or removed from the session
scope altogether.
Prof. AshishSingh Bhatia 22
23. Task ¨C Self Study
? Suppose you want to track buying patterns for a specific item (a yacht, in
this case). Of course, you could try to find all servlets and JSP pages that
process orders and change each one to record yacht purchases. That¡¯s an
awful lot of work for what sounds like a simple request, though, and pretty
hard to maintain, anyhow. A much better option is to create a session
attribute listener that monitors the attributes corresponding to order
reservations or purchases and that records the information in the log file for
later perusal by the sales manager.
Prof. AshishSingh Bhatia 23