TECHNICALFACILITATION.COM - THE ULTIMATE WEBSPHERE AND JAVA CERTIFICATION GUIDES AND RESOURCES
Google
Download the Completed Solution: 09LifeCycleListeners.ear.

Scroll to the bottom of the page for pertinent code snippets.

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");
}

}

  <?xml version="1.0" encoding="UTF-8" ?>
- <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <display-name>ExamScamWeb</display-name>
- <listener>
  <description>MyKewlListener</description>
  <display-name>MyKewlListener</display-name>
  <listener-class>com.examscam.web.listener.MyKewlListener</listener-class>
  </listener>
- <servlet>
  <description />
  <display-name>CountrySnooper</display-name>
  <servlet-name>CountrySnooper</servlet-name>
  <servlet-class>com.examscam.servlet.CountrySnooper</servlet-class>
  </servlet>
- <servlet>
  <description />
  <display-name>SessionServlet</display-name>
  <servlet-name>SessionServlet</servlet-name>
  <servlet-class>com.examscam.servlet.SessionServlet</servlet-class>
  </servlet>
- <servlet>
  <description />
  <display-name>NumberGuesserServlet</display-name>
  <servlet-name>NumberGuesserServlet</servlet-name>
  <servlet-class>com.examscam.servlet.NumberGuesserServlet</servlet-class>
  </servlet>
- <servlet-mapping>
  <servlet-name>CountrySnooper</servlet-name>
  <url-pattern>/CountrySnooper</url-pattern>
  </servlet-mapping>
- <servlet-mapping>
  <servlet-name>SessionServlet</servlet-name>
  <url-pattern>/SessionServlet</url-pattern>
  </servlet-mapping>
- <servlet-mapping>
  <servlet-name>NumberGuesserServlet</servlet-name>
  <url-pattern>/NumberGuesserServlet</url-pattern>
  </servlet-mapping>
- <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
- <jsp-config>
- <taglib>
  <taglib-uri>/WEB-INF/examscam-taglib.tld</taglib-uri>
  <taglib-location>/WEB-INF/examscam-taglib.tld</taglib-location>
  </taglib>
  </jsp-config>
  </web-app>

<<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<HTML>
<HEAD>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" session="true"%>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM Software Development Platform">
<META http-equiv="Content-Style-Type" content="text/css">
<LINK href="theme/Master.css" rel="stylesheet"
type="text/css">
<TITLE>listenerProover.jsp</TITLE>
</HEAD>
<BODY>
Hello Cruel World:

<%
try {
out.println("<BR>The magic key placed in your session: ");
request.getSession(true);
out.println(session.getAttribute("magicKey"));
out.println("<BR><BR>Timestamp in the context" + application.getAttribute("timestamp"));
session.invalidate();
}
catch (Exception e ) {
System.out.println(e.getClass() + e.getMessage());
}
%>

</BODY>


</HTML>
Google
THE ULTIMATE CERTIFICATION AND WEBSPHERE RESOURCES - BUY THEM NOW ON AMAZON
eXTReMe Tracker