Servlet Event framework
Upcoming SlideShare
Loading in...5

Servlet Event framework



Learn different listeners associated with servlet life cycle.

Learn different listeners associated with servlet life cycle.



Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds


Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Servlet Event framework Servlet Event framework Presentation Transcript

  • APPLICATION EVENT FRAMEWORKProf. AshishSingh Bhatia1Prof. 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 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
  • 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!!!!
  • Monitoring creation and destruction of Servlet ContextProf. AshishSingh Bhatia 7
  • web.xmlProf. AshishSingh Bhatia 8
  • JSP FileProf. AshishSingh Bhatia 9
  • Take a new caseProf. AshishSingh Bhatia 10What if servlet changethe companyName ofcontext-param ?Example :
  • Take a new caseProf. AshishSingh Bhatia 11We should ensure that when companyname is changedformerCompanyName should also bechangedcompanyName = asbspace.informerCompanyName=test.comServletContextAttributeListener
  • HTML FormProf. AshishSingh Bhatia 12
  • Servlet Changing NameProf. AshishSingh Bhatia 13
  • ServletContextAttributeListenerProf. AshishSingh Bhatia 14
  • JSP displaying new and old name of the companyProf. AshishSingh Bhatia 15
  • 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
  • Prof. AshishSingh Bhatia 17
  • Prof. AshishSingh Bhatia 18test.jspmakesession.html
  • sessioncount.jspProf. AshishSingh Bhatia 19
  • Prof. AshishSingh Bhatia 20Cookies must be disabled. So eachjsp load will act as a request andcreate a new session
  • sessioncount.jsp outputProf. 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 thefirst time, replaced by another object, or removed from the sessionscope altogether.Prof. AshishSingh Bhatia 22
  • 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
  • Calculating Server Request LoadProf. AshishSingh Bhatia 24
  • END OF SESSION25Prof. AshishSingh Bhatia