servlet event framework

25
APPLICATION EVENT FRAMEWORK Prof. AshishSingh Bhatia 1 Prof. AshishSingh Bhatia

Upload: ashishsingh-bhatia

Post on 17-May-2015

597 views

Category:

Education


1 download

DESCRIPTION

Learn different listeners associated with servlet life cycle.

TRANSCRIPT

Page 1: Servlet Event framework

APPLICATION EVENT FRAMEWORK

Prof. AshishSingh Bhatia

1Prof. AshishSingh Bhatia

Page 2: Servlet Event framework

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

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

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

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

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

Monitoring creation and destruction of Servlet Context

Prof. AshishSingh Bhatia 7

Page 8: Servlet Event framework

web.xml

Prof. AshishSingh Bhatia 8

Page 9: Servlet Event framework

JSP File

Prof. AshishSingh Bhatia 9

Page 10: Servlet Event framework

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

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

HTML Form

Prof. AshishSingh Bhatia 12

Page 13: Servlet Event framework

Servlet Changing Name

Prof. AshishSingh Bhatia 13

Page 14: Servlet Event framework

ServletContextAttributeListener

Prof. AshishSingh Bhatia 14

Page 15: Servlet Event framework

JSP displaying new and old name of the company

Prof. AshishSingh Bhatia 15

Page 16: Servlet Event framework

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

Prof. AshishSingh Bhatia 17

Page 18: Servlet Event framework

Prof. AshishSingh Bhatia 18

test.jsp

makesession.html

Page 19: Servlet Event framework

sessioncount.jsp

Prof. AshishSingh Bhatia 19

Page 20: Servlet Event framework

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

sessioncount.jsp output

Prof. AshishSingh Bhatia 21

Page 22: Servlet Event framework

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

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

Calculating Server Request Load

Prof. AshishSingh Bhatia 24

Page 25: Servlet Event framework

END OF SESSION

25Prof. AshishSingh Bhatia