ServletContextListener Example

ServletContextListener Example explains about How to use ServletContextListener in a web application.

The listener ServletContextListener interface for receiving notification events about ServletContext lifecycle changes.

For the entire web application, there will be only one ServletContext. 

ServletContext is created when a new deployment is made after that this ServletContext will be used by all the jsp's and servlet's in the web application.

ServletContextListener has the following two methods:

public void contextInitialized(ServletContextEvent event)
public void contextDestroyed(ServletContextEvent event)

Package Structure

ServletContextListener Example

web.xml

<?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"
    version="2.5">
    <listener>
        <listener-class>com.listener.ServletContextListenerExample</listener-class>
    </listener>
    <servlet>
        <servlet-name>ServletContextListenerTest</servlet-name>
        <servlet-class>com.servlet.ServletContextListenerTest</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>ServletContextListenerTest</servlet-name>
        <url-pattern>/ServletContextListenerTest</url-pattern>
    </servlet-mapping>
</web-app>

ServletContextExample.java

package com.listener;

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

public class ServletContextListenerExample implements ServletContextListener {
 
ServletContext context;

 
public void contextInitialized(ServletContextEvent contextEvent) {
   
context = contextEvent.getServletContext();
   
// set variable to servlet context
   
context.setAttribute("JavaTips.net", "JavaTips.net");
 
}

 
public void contextDestroyed(ServletContextEvent contextEvent) {
   
this.context = null;
 
}
}

ServletContextListenerTest.java

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletContextListenerTest extends HttpServlet {
 
private static final long serialVersionUID = 1L;
      
 
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
response.setContentType("text/html");
    PrintWriter pw = response.getWriter
();
    pw.println
("The attribute available is : " + getServletContext().getAttribute("JavaTips.net"));
 
}
}
ServletContextListener Output
Note

From Servlet API 3.0, we can use javax.servlet.annotation, so that web.xml is not necessary

Here we are using @WebListener annotation

ServletContextListenerExample.java

package com.listener;

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

@WebListener
public class ServletContextListenerExample implements ServletContextListener {
 
ServletContext context;

 
public void contextInitialized(ServletContextEvent contextEvent) {
   
context = contextEvent.getServletContext();
   
// set variable to servlet context
   
context.setAttribute("JavaTips.net", "JavaTips.net");
 
}

 
public void contextDestroyed(ServletContextEvent contextEvent) {
   
this.context = null;
 
}
}

ListenerServletExample.java

package com.listener;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
* This servlet is used for showing an example about sessionListener
*/
@WebServlet("/ListenerServletExample")
public class ListenerServletExample extends HttpServlet {
   
private static final long serialVersionUID = 1L;

  
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      
      
response.setContentType("text/html");
        PrintWriter pw = response.getWriter
();
        pw.println
("The attribute available is : " + getServletContext().getAttribute("JavaTips.net"));
      
   
}
}

Output

The attribute available is : JavaTips.net 








Your email address will not be published. Required fields are marked *