SET AS HOME PAGE

ADD TO FAVORITES / BOOKMARK THIS WEBSITE (More Details)

Introduction

Servlet

Jsp

Security

Enterprise Beans

Contact Us


Implementing Servlet Life Cycle Events

 Introduction

Various event listener interfaces are:
  • ServletRequestListener Interface
  • ServletRequestAttributeListener Interface
  • ServletContextListener interface
  • ServletContextAttributeListener Interface
  • HttpSessionListener Interface
  • HttpSessionAttributeListener Interface
  • HttpSessionActivationListener Interface

The various events that are generated during the life cycle of a servlet are:
  • Servlet request events
  • Servlet context events
  • HTTP session events
Example for Implementing Servlet Life Cycle Events

CLICK HERE to download this complete example (zip file)


 Create a Servlet that Adds Attributes to the Context Objects

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class ServletEvents extends HttpServlet
{
   ServletContext ctx;
    PrintWriter pw;
    public void init(ServletConfig cfig)
    {
       ctx = cfig.getServletContext();
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
       ctx.setAttribute("URL","jdbc:odbc:EmployeesDB");
       pw = response.getWriter();
       response.setContentType("text/html");
       pw.println("<B>The JDBC URL has been set as a context attribute at " + new Date() + "</B></BR>");
       ctx.removeAttribute("URL");
       pw.println("<B>The JDBC URL has been removed from context at " + new Date() + "</B>");
    }
}

Download ServletEvents.java

 Create a Servlet that Logs the Timer of Events

import javax.servlet.*;
import java.io.*;
import java.util.*;
public class ServletEventListener implements ServletContextListener, ServletContextAttributeListener, ServletRequestListener
{
    ServletContext contx;
    String name;
    String value;
    public void contextInitialized(ServletContextEvent ce)
    {
       contx=ce.getServletContext();
       contx.log("Context has been initialized at " + new Date());
    }
    public void contextDestroyed(ServletContextEvent ce)
    {
       contx.log("Context has been destroyed at " + new Date());
    }
    public void attributeAdded(ServletContextAttributeEvent srae)
    {
       name=srae.getName();
       value=(String)srae.getValue();
       contx.log("An attribute with name: " + name + " and value: " + value + " has been added to the context at: " + new Date());
    }
    public void attributeRemoved(ServletContextAttributeEvent srae)
    {
       contx.log("Attribute with name: " + name + " and value: " + value + " has been removed from the context at: " + new Date());
    }
    public void attributeReplaced(ServletContextAttributeEvent srae)
    {
       contx.log("Attribute with name: " + name + " and value: " + value + " has been replaced context at: " + new Date());
    }
    public void requestInitialized(ServletRequestEvent e)
    {
       contx.log("A request has been initialized at: " + new Date());
    }
    public void requestDestroyed(ServletRequestEvent e)
    {
       contx.log("A request has been destroyed at: " + new Date());
    }
}

Download ServletEventListener.java

 Specify the initialization parameters
  1. Write a java program and name it as ServletEvents.html
  2. Write a java program and name it as ServletEventListener.java
  3. Set the path in the command prompt
    set path=.;C:\progra~1\java\j2sdk1.5.0\bin;C:\Sun\AppServer\bin;
    Set classpath=.;C:\progra~1\java\j2sdk1.5.0\lib;C:\Sun\AppServer\lib\j2ee.jar;
    (OR)
    Set the path in the system itself. CLICK HERE for details
  4. Now compile the ServletEvents.java and ServletEventListener.java files. CLICK HERE to see how to compile
  5. Goto Start->Programs->Sun Microsystems->Application Server PE->Start Default Server (Wait till it start and then press any key). CLICK HERE to see how to Start the Server
  6. Goto Start->Programs-> Sun Microsystems->Application Server PE->Deploytool. CLICK HERE to see how to Start the Deploytool
  7. Goto File ->New -> Application     Note:    Inserted of EmployeeDetails use Listener

    (Click the Browse button)


  8. (Select the folder in the Look In dropdown box, and then give a file name “Listener”. Next click the New Application button)


  9. (Click the OK button)
  10. Now goto File -> Save to save the file
  11. Next, goto File -> New -> Web Component

    (Click Next button)


  12. (Enter the WAR Name as “EmpApp” and then click the Edit Contents… button)


  13. (Select all the .class, .jsp , .tld and .html files and click the Add button)


  14. (Now click the OK button)


  15. (Now click the Next button)


  16. (Now select the Servlet option button and then click the Next button)


  17. (Now select the “ServletEvents” from the Servlet Class dropdown box)


  18. (Now select the Next button)


  19. (Now select the Finish button)


  20. (Now select the EmpApp in the left pane and select the General tab in the right pane. Here give a name “example3” in the Context Root text box)
  21. Next select the InitParameterDemo in the right side

    (Now select the ServletEvents in the left pane and then select the Aliases tab in the right pane. Next select the Add button)


  22. (Now add a name as “ServletEvents”)

  23. Now select WebApp in the left pan and then select the Event Listeners tab in the right pane
  24. Now click the Add button and then in the Event Listener Classes pane select the ServletEventListener
  25. Now goto File ->Save
  26. Next goto Tools -> Deployee

    (Enter the User Name as “admin” and Password as “password” (CLICK HERE for password). Next click the OK button)


  27. (Now a message --- Operation Completed Successfully --- must display. Next click the Close button)
  28. Next goto File -> Exit to close it
  29. Now open an Internet Explorer and type the address http:// localhost:8080/example3/ServletEvents

  30. The contents of the server.log file is shown in the figure (E:\Sun\AppServer\domains\domain1\logs\server.log)

  31. Program completed Successfully
  32. To stop the server goto Start -> All Programs -> Sun Microsystems -> Application Server PE -> Stop Default Server. CLICK HERE to see how to Stop the Server

 Click for Next Topic
<- PREVIOUSNEXT ->