Pin It

How to create a Listener using @WebListener annotation ( ServletContextListener ) in Java EE application ?.

Program to demonstrate creation of a ServletContextListener using @WebListener annotation.

package com.hubberspot.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

// @WebListener annotation informs container that
// this class is a web listener which will listen to
// various events happening during lifecycle of application
// Here this class listens to StartUp and ShutDown of the
// application.

// We make class implements ServletContextListener which has two
// methods contextInitialized() and contextDestroyed() , which
// are called by the container whenever a servlet context is 
// started or shutdown

public class StartStopAppListener implements ServletContextListener {

 public void contextInitialized(ServletContextEvent servletContextEvent) {

  System.out.println("Servlet Context Initialized ... ");


 public void contextDestroyed(ServletContextEvent servletContextEvent) {

  System.out.println("Servlet Context Destroyed ... ");



Output of the program : 


Please Give Us Your 1 Minute In Sharing This Post!
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
© 2012 Learn Java by Examples Template by Hubber Spot