Creating Lifecycle Listeners
This free, multimedia tutorial shows you how to use IBM's Rational Application Developer (IRAD) 6.0 to create
a lifecycle listener that will respond to the creation of an HttpSession, or the initializing of a
web application. In other words, the component created implements the HttpSessionListener and the
ServletContextListener interfaces. How's that for excitement?
If you found something helpful here, please do your part and help support the site. Link to us, buy some books, support our sponsors, tell your developer friends about us, and remember: Happy Java!
|
package com.examscam.web.listener;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener;
public class MyKewlListener implements ServletContextListener, HttpSessionListener {
public void contextInitialized(ServletContextEvent contextEvent) { contextEvent.getServletContext().setAttribute("timestamp", new java.util.Date()); System.out.println("Context Initialized"); }
public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub }
public void sessionCreated(HttpSessionEvent sessionEvent) { String magicKey = ""+ System.currentTimeMillis() % 100; sessionEvent.getSession().setAttribute("magicKey", magicKey); System.out.println("Session Initialized"); }
/* (non-Java-doc) * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(HttpSessionEvent arg0) */ public void sessionDestroyed(HttpSessionEvent arg0) { System.out.println("Session Destroyed"); }
}
|