@WebListener Annotation Example in Servlet 3 with ServletContextListener

By Arvind Rai, January 14, 2015
@WebListener annotation has been introduced in Java EE 6 to declare a class as listener. There are different listener interfaces in servlet such as ServletContextListener, ServletContextAttributeListener, HttpSessionListener, HttpSessionAttributeListener. To create our own listener we have to implement any of the listener interfaces and annotate class by @WebListener annotation. In our example we are creating our listener implementing ServletContextListener.

web.xml

We are adding some initialization parameter in web.xml. Find the web.xml
 <?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app 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"
    version="3.0">
	<display-name>Servlet 3</display-name>
    <context-param>
  	  <param-name>path</param-name>
  	  <param-value>C:/filePath</param-value>
    </context-param>
    <context-param>
  	  <param-name>mode</param-name>
  	  <param-value>read</param-value>
    </context-param>
</web-app> 

Create Listener for ServletContextListener

In our example we are creating listener using ServletContextListener. To create listener, we create a class annotated with @WebListener and implement ServletContextListener. We have to override the methods contextInitialized(ServletContextEvent sce) and contextDestroyed(ServletContextEvent sce) . In our example, we will fetch init parameter from web.xml and will set it in servlet context.
MyListener.java
package com.concretepage;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class MyListener implements ServletContextListener {
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		ServletContext sc = sce.getServletContext();
		String path = sc.getInitParameter("path");
		String mode = sc.getInitParameter("mode");
		sc.setAttribute("filePath", path);
		sc.setAttribute("fileMode", mode);
		System.out.println("Value saved in context.");
	}
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		ServletContext sc = sce.getServletContext();
		sc.removeAttribute("path");
		sc.removeAttribute("mode");
		System.out.println("Value deleted from context.");
	}
} 

Create Servlet

We are creating a servlet and accessing the attribute set by listener.
MyServlet.java
package com.concretepage;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = "/HelloWorldServlet", loadOnStartup = 1)
public class MyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
	     doGet(request,response);
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
	     response.setContentType("text/html");
	     ServletContext sc = request.getServletContext();
	     System.out.println("Path:"+ sc.getAttribute("filePath"));
	     System.out.println("Mode:"+ sc.getAttribute("fileMode"));
	     PrintWriter out = response.getWriter();
	     out.println("Hello World!");
	}
} 

Output

Our ServletContextListener will be processed at server start and contextInitialized method will run and output will be as below.
 ------------------
 ------------------
 Value saved in context.
 ------------------
 ------------------ 
When we access servlet by the URL http://localhost:8080/ServletDemo-1/HelloWorldServlet , then output will be as below.
Path:C:/filePath
Mode:read 
When we stop servlet our listener will run its contextDestroyed method.
 ------------------
 ------------------
Value deleted from context.
 ------------------
 ------------------
 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us