Home Servlets ServletContextListener example
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

ServletContextListener example

Advertisement
Before going into the details of ServletContextListener we should understand what is ServletContext. ServletContext is a interface which helps us to communicate with the servlet container.

ServletContextListener example

     

Before going into the details of  ServletContextListener we should understand what is ServletContext. ServletContext is a interface which helps us to communicate with the servlet container. There is only one ServletContext for the entire web application and the components of the web application can share it. The information in the ServletContext will be common to all the components. Remember that each servlet will have its own ServletConfig. The ServetContext is created by the container when the web application is deployed and after that only the context is available to each servlet in the web application.

ServletContextListener is a interface which contains two methods:

  1. public void contextInitialized(ServletContextEvent event)
  2. public void contextDestroyed(ServletContextEvent event)

When we implement any interface then we have to implement its all methods. This listener will help a application to start and shutdown the events.

How  the ServletContextListener is useful:

1. ServletContextListener is notified when the context is initialized.

a). ServletContextListener gets the context init parameters from the ServletContext.

b). It stores the database connection as an attribute, so that the other components in the web application can access it.

2. It will be notified when the context is destroyed. It closes the database connection.

The code of the program is given below:

 

import javax.servlet.*;
import javax.servlet.http.*;

public class MyServletContextListener implements ServletContextListener{
public void contextInitialized(ServletContextEvent event)
{
ServletContext sc = event.getServletContext();
String whatType = sc.getInitParameter("typeSelected");
Furniture f = new Furniture(whatType);
sc.setAttribute("furniture", f);
}
public void contextDestroyed(ServletContextEvent event)
{

}
}

 

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ListenerTester extends javax.servlet.http.HttpServlet 
implements javax.servlet.Servlet {
public ListenerTester() {
super();


public void doGet(HttpServletRequest request, }
HttpServletResponse response) throws 
ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("context attributes set by the listener <br>");
Furniture f = (Furniture)getServletContext().getAttribute("furniture");
pw.println("The furniture you have selected is :" + f.getTypeSelected());


public void doPost(HttpServletRequest request, 
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

}

The output of the program is given below:

Download this example.

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 12, 2008

Ask Questions?    Discuss: ServletContextListener example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Tarn.Keks
May 31, 2011
great article!

Thanks for this great article. I just started to deal with Servlets and GWT.
Sunil Pachori
June 7, 2011
defiined servlet context listener in web.xml

how to defined the <listener-class>path of servlet </listener-class> in web.xml
chetan
November 9, 2011
Not getting what to do ...

I am not getting what is 'Furniture' here ..So plz give me detail information on it... ServletContext sc = event.getServletContext(); String whatType = sc.getInitParameter("typeSelected"); Furniture f = new Furniture(whatType); sc.setAttribute("furniture", f);
John
February 28, 2013
Issue

Not a complete description. Furniture f = new Furniture(whatType); // Mean ? Furniture f = (Furniture)getServletContext().getAttribute("furniture"); //Not clear
Nagi
February 12, 2012
ServletContextListener

Thanks alot, simple and good explination
munna
June 8, 2012
furniiture

no funiture class
DMCA.com