Saturday, July 25, 2009

Create Application Lifecycle Listener using Eclipse

http://help.eclipse.org/ganymede/index.jsp?topic=/org.eclipse.wst.webtools.doc.user/topics/cwebartifact.html

Create listener using wizard



Select listener type



package testEAR;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
* Application Lifecycle Listener implementation class TestApplicationLifecycleListener
*
*/
public class TestApplicationLifecycleListener implements ServletContextListener {

/**
* Default constructor.
*/
public TestApplicationLifecycleListener() {
System.out.println("TestApplicationLifecycleListener constructor");
}

/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("TestApplicationLifecycleListener initialize event" + arg0);
}

/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("TestApplicationLifecycleListener destroy event" + arg0);
}

}



web.xml descriptor
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>testDynamicWeb</display-name>
<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>
<servlet>
<description></description>
<display-name>TestServlet</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>testEAR.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/sayHello</url-pattern>
</servlet-mapping>
<listener>
<listener-class>testEAR.TestApplicationLifecycleListener</listener-class>
</listener>

</web-app>



TestApplicationLifecycleListener constructor
TestApplicationLifecycleListener initialize eventjavax.servlet.ServletContextEvent[source=weblogic.servlet.internal.WebAppServletContext@194737d
- appName: 'testEAR', name: 'testDynamicWeb', context-path: '/testDynamicWeb', spec-version: '2.5']
...
TestApplicationLifecycleListener destroy eventjavax.servlet.ServletContextEvent[source=weblogic.servlet.internal.WebAppServletContext@194737d
- appName: 'testEAR', name: 'testDynamicWeb', context-path: '/testDynamicWeb', spec-version: '2.5']


No comments:

Post a Comment