Servlet Event framework


Published on

Learn different listeners associated with servlet life cycle.

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Servlet Event framework

  1. 1. APPLICATION EVENT FRAMEWORKProf. AshishSingh Bhatia1Prof. AshishSingh Bhatia
  2. 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
  3. 3. Listeners Interface ServletContextListener ServletContext AttributeListener HttpSessionListener HttpSessionAttributeListener HttpSessionActivationListener HttpSessionBindingListener ServletRequest Listener [ 2.4 ] ServletRequestAttributeListener [ 2.4 ]Prof. AshishSingh Bhatia 3
  4. 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. 5. Take a Case Study and Think out of Box We are enterprise level [ Huge Company with lot many pages of webapplication ] 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. 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></param-value></context-param> In Servlet we will read it usingString name = getServletContext().getInitParameter(“companyName”);Prof. AshishSingh Bhatia 6Guess we have 100 [ Servlet / JSP ]If we forgot to add entry in web.xml ?Every Servlet / Page will display null!!!!
  7. 7. Monitoring creation and destruction of Servlet ContextProf. AshishSingh Bhatia 7
  8. 8. web.xmlProf. AshishSingh Bhatia 8
  9. 9. JSP FileProf. AshishSingh Bhatia 9
  10. 10. Take a new caseProf. AshishSingh Bhatia 10What if servlet changethe companyName ofcontext-param ?Example :
  11. 11. Take a new caseProf. AshishSingh Bhatia 11We should ensure that when companyname is changedformerCompanyName should also bechangedcompanyName = asbspace.informerCompanyName=test.comServletContextAttributeListener
  12. 12. HTML FormProf. AshishSingh Bhatia 12
  13. 13. Servlet Changing NameProf. AshishSingh Bhatia 13
  14. 14. ServletContextAttributeListenerProf. AshishSingh Bhatia 14
  15. 15. JSP displaying new and old name of the companyProf. AshishSingh Bhatia 15
  16. 16. Session Related Listener For all Sessions ServletContextListener ServletContextAttributeListener For Specific Sessions HttpSessionListener HttpSessionAttributeListener Counting number of sessions HttpSessionListener Interface : sessionCreated and sessionDestroyedProf. AshishSingh Bhatia 16
  17. 17. Prof. AshishSingh Bhatia 17
  18. 18. Prof. AshishSingh Bhatia 18test.jspmakesession.html
  19. 19. sessioncount.jspProf. AshishSingh Bhatia 19
  20. 20. Prof. AshishSingh Bhatia 20Cookies must be disabled. So eachjsp load will act as a request andcreate a new session
  21. 21. sessioncount.jsp outputProf. AshishSingh Bhatia 21
  22. 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 thefirst time, replaced by another object, or removed from the sessionscope altogether.Prof. AshishSingh Bhatia 22
  23. 23. Task – Self Study Suppose you want to track buying patterns for a specific item (a yacht, inthis case). Of course, you could try to find all servlets and JSP pages thatprocess orders and change each one to record yacht purchases. That’s anawful lot of work for what sounds like a simple request, though, and prettyhard to maintain, anyhow. A much better option is to create a sessionattribute listener that monitors the attributes corresponding to orderreservations or purchases and that records the information in the log file forlater perusal by the sales manager.Prof. AshishSingh Bhatia 23
  24. 24. Calculating Server Request LoadProf. AshishSingh Bhatia 24
  25. 25. END OF SESSION25Prof. AshishSingh Bhatia