Download - Servlet Event framework
![Page 1: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/1.jpg)
APPLICATION EVENT FRAMEWORK
Prof. AshishSingh Bhatia
1Prof. AshishSingh Bhatia
![Page 2: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/2.jpg)
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
![Page 3: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/3.jpg)
Listeners Interface
ServletContextListener
ServletContext AttributeListener
HttpSessionListener
HttpSessionAttributeListener
HttpSessionActivationListener
HttpSessionBindingListener
ServletRequest Listener [ 2.4 ]
ServletRequestAttributeListener [ 2.4 ]
Prof. AshishSingh Bhatia 3
![Page 4: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/4.jpg)
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
![Page 5: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/5.jpg)
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
![Page 6: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/6.jpg)
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 !!!!
![Page 7: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/7.jpg)
Monitoring creation and destruction of Servlet Context
Prof. AshishSingh Bhatia 7
![Page 8: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/8.jpg)
web.xml
Prof. AshishSingh Bhatia 8
![Page 9: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/9.jpg)
JSP File
Prof. AshishSingh Bhatia 9
![Page 10: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/10.jpg)
Take a new case
Prof. AshishSingh Bhatia 10
What if servlet change the companyName of
context-param ?Example : asbspace.in
![Page 11: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/11.jpg)
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
![Page 12: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/12.jpg)
HTML Form
Prof. AshishSingh Bhatia 12
![Page 13: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/13.jpg)
Servlet Changing Name
Prof. AshishSingh Bhatia 13
![Page 14: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/14.jpg)
ServletContextAttributeListener
Prof. AshishSingh Bhatia 14
![Page 15: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/15.jpg)
JSP displaying new and old name of the company
Prof. AshishSingh Bhatia 15
![Page 16: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/16.jpg)
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
![Page 17: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/17.jpg)
Prof. AshishSingh Bhatia 17
![Page 18: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/18.jpg)
Prof. AshishSingh Bhatia 18
test.jsp
makesession.html
![Page 19: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/19.jpg)
sessioncount.jsp
Prof. AshishSingh Bhatia 19
![Page 20: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/20.jpg)
Prof. AshishSingh Bhatia 20
Cookies must be disabled. So each jsp load will act as a request and
create a new session
![Page 21: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/21.jpg)
sessioncount.jsp output
Prof. AshishSingh Bhatia 21
![Page 22: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/22.jpg)
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
![Page 23: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/23.jpg)
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
![Page 24: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/24.jpg)
Calculating Server Request Load
Prof. AshishSingh Bhatia 24
![Page 25: Servlet Event framework](https://reader033.vdocuments.us/reader033/viewer/2022052823/555816add8b42a5e468b4aa0/html5/thumbnails/25.jpg)
END OF SESSION
25Prof. AshishSingh Bhatia