servlet event framework
DESCRIPTION
Learn different listeners associated with servlet life cycle.TRANSCRIPT
APPLICATION EVENT FRAMEWORK
Prof. AshishSingh Bhatia
1Prof. AshishSingh Bhatia
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
Listeners Interface
ServletContextListener
ServletContext AttributeListener
HttpSessionListener
HttpSessionAttributeListener
HttpSessionActivationListener
HttpSessionBindingListener
ServletRequest Listener [ 2.4 ]
ServletRequestAttributeListener [ 2.4 ]
Prof. AshishSingh Bhatia 3
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
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
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 !!!!
Monitoring creation and destruction of Servlet Context
Prof. AshishSingh Bhatia 7
web.xml
Prof. AshishSingh Bhatia 8
JSP File
Prof. AshishSingh Bhatia 9
Take a new case
Prof. AshishSingh Bhatia 10
What if servlet change the companyName of
context-param ?Example : asbspace.in
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
HTML Form
Prof. AshishSingh Bhatia 12
Servlet Changing Name
Prof. AshishSingh Bhatia 13
ServletContextAttributeListener
Prof. AshishSingh Bhatia 14
JSP displaying new and old name of the company
Prof. AshishSingh Bhatia 15
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
Prof. AshishSingh Bhatia 17
Prof. AshishSingh Bhatia 18
test.jsp
makesession.html
sessioncount.jsp
Prof. AshishSingh Bhatia 19
Prof. AshishSingh Bhatia 20
Cookies must be disabled. So each jsp load will act as a request and
create a new session
sessioncount.jsp output
Prof. AshishSingh Bhatia 21
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
Task – 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
Calculating Server Request Load
Prof. AshishSingh Bhatia 24
END OF SESSION
25Prof. AshishSingh Bhatia