ServletContextListener Exmaple

ServletContextListener Exmaple


Posted in : Java Posted on : June 21, 2011 at 4:30 PM Comments : [ 0 ]

n this section we will discuss about an interface SevletContextListener.

ServletContextListener Exmaple

In this section we will discuss about an interface SevletContextListener.

Before doing further example let we will first discussed about the ServleContextListener. The ServletContextListener is an interface that receives notification of the alters in the event of ServletContext lifecycle. For receiving these notification events there must an implemented class be either stated into the deployment descriptor or registered with one of the ServletContext's addListener methods.

This interface has the two methods which you will have to use whenever you will be implemented it. These two methods are :

public void contextInitialized(ServletContextEvent e) : It gets web application initialization initiating notice.

public void contextDestroyed(ServletContextEvent e) : It gets ServletContext notice that it is about to be shut down.

Example :

Student.java

public class Student {
String name = "";
String add = "";

public Student(String name, String add)
{
this.name=name;
this.add = add;
}
public String getAdd()
{
return add;
}
public String getName() {

return name;
}
}

ContextListenerExample.java

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

public class ContextListenerExample implements ServletContextListener {
public void contextInitialized(ServletContextEvent e)
{
ServletContext cntxt = e.getServletContext();
Student stu = new Student("Bipul", "Rohini");
Student stu1 = new Student("Vinay", "Rohini");
cntxt.setAttribute("stu", stu);
cntxt.setAttribute("stu1", stu1);
}
public void contextDestroyed(ServletContextEvent e)
{
System.out.println("Destroyed");
}
}

ListenerExample.java

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 ListenerExample extends HttpServlet 
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
pw.println("<h2>Student's Name and Address</h2>");
pw.println("");
Student stu = (Student)getServletContext().getAttribute("stu");
Student stu1 = (Student)getServletContext().getAttribute("stu1");
pw.println("\n<b>"+stu.getName()+"</b> lives in <b>"+stu.getAdd()+"</b><br>");
pw.println("\n<b>"+stu1.getName()+"</b> lives in <b>"+ stu.getAdd()+"</b><br>");
}
}

web.xml

<web-app>
<listener>
<listener-class>ContextListenerExample</listener-class>
</listener>
<servlet>
<servlet-name>ListenerExample</servlet-name>
<servlet-class>ListenerExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ListenerExample</servlet-name>
<url-pattern>/ListenerExample</url-pattern>
</servlet-mapping>
</web-app>

Output : When you will execute this example then the following output will be generated.

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics