Tuesday, March 22, 2011

Chapter 19: Listeners & Interfaces in Web Context

We have seen a lot of things related to a web application like the HttpRequest, ServletContext etc. there are a lot of listeners and interfaces that are part of any J2EE applications life. In this chapter we are going to take a look at them.

So, lets get started!!!

What are Listeners Used For?

Listeners are a useful mechanism by which we can monitor and react to servlet events by defining the appropriate listener objects. These objects have methods that the container would invoke when the life-cycle event occurs. To make this happen, you define a listener class by implementing a listener interface. The container would invoke the listener method and pass it information about that event.

For ex: When the HttpSessionListener interface is implemented, the methods will be passed an HttpSessionObject

What are these Listeners?

There are many different types of Listeners that are available for us to use. The listener we can use depends on the purpose it serves. The Listeners and their purpose are as follows:

1. When a Servlet is Initialized or Destroyed
     a. javax.servlet.ServletContextListener
     b. contextDestroyed(ServletContextEvent sce) Notification that the servlet context is about to be shut down
     c. contextInitialized(ServletContextEvent sce) Notification that the Web application is ready to process requests
2. When a Context attribute is added/removed or replaced
     a. javax.servlet.ServletContextAttributeListener.
     b. attributeAdded(ServletContextAttributeEvent scab) Notification that a new attribute was added to the servlet context.
     c. attributeRemoved(ServletContextAttributeEvent scab) Notification that an existing attribute has been removed from the servlet context.
     d. attributeReplaced(ServletContextAttributeEvent scab) Notification that an attribute on the servlet context has been replaced
3. When a session is initialized or destroyed
     a. javax.servlet.http.HttpSessionListener.
     b. sessionCreated(HttpSessionEvent se) Notification that a session was created.
     c. sessionDestroyed(HttpSessionEvent se) Notification that a session became invalid or timed out.
4. When a session attribute is added/removed or replaced
     a. HttpSessionAttributeListener.
     b. attributeAdded(HttpSessionBindingEvent se) Notification that an attribute has been added to a session.
     c. attributeRemoved(HttpSessionBindingEvent se) Notification that an attribute has been removed from a session.
     d. attributeReplaced(HttpSessionBindingEvent se) Notification that an attribute has been replaced in a session

A Sample Listener Class

Let us now take a look at a sample class that is going to listen for context initialization and destruction. The attribute StartDate is set when the container initializes the application. Then when the application quits, the same attribute is logged and then deleted. This is just a simple example so we arent doing anything major here. But in realtime, we may end up putting a lot more code in these methods than what you see below…

Code:

import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public final class ContextListener
implements ServletContextListener
{
public void contextInitialized(
ServletContextEvent event)
{
ServletContext context = event.getServletContext();
context.setAttribute("StartDate", Date);
}

public void contextDestroyed(ServletContextEvent event)
{
ServletContext context = event.getServletContext();
Date startDate = context.getAttribute("StartDate");
customLog(startDate);
context.removeAttribute("StartDate");
}
}

Similarly we can have classes that listen for various other events and handle them appropriately…

Previous Chapter: Chapter 18 - Using Request Dispatcher

Next Chapter: Quick Recap - Chapters 6 to 19

No comments:

Post a Comment

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers