logo

ServletContextEvent And ServletContextListener


Show

The ServletContextEvent is notified while Web utility is deployed at the server.

If you need to carry out a few movements on the time of deploying the net utility inclusive of growing database connection, growing all of the tables of the venture etc, you want to put into effect ServletContextListener interface and offer the implementation of its techniques.

Constructor of ServletContextEvent magnificence

There is one handiest constructor described withinside the ServletContextEvent magnificence. The net field creates the example of ServletContextEvent after the ServletContext instance.

  1. ServletContextEvent(ServletContext e)

Method of ServletContextEvent magnificence

There is handiest one approach described withinside the ServletContextEvent magnificence:

  1. public ServletContext getServletContext(): returns the example of ServletContext.

Methods of ServletContextListener interface

There are techniques declared withinside the ServletContextListener interface which need to be carried out via way of means of the servlet programmer to carry out a few movements inclusive of growing database connection etc.

  1. public void contextInitialized(ServletContextEvent e): is invoked while utility is deployed at the server.
  2. public void contextDestroyed(ServletContextEvent e): is invoked while utility is undeployed from the server.

Example of ServletContextEvent and ServletContextListener

In this example, we're retrieving the facts from the emp32 table. To serve this, we've created the relationship item withinside the listener magnificence and used the relationship item withinside the servlet.

Index.html

<a href="servlet1">fetch records</a> 

Mylistener.java

import javax.servlet.*;  
import java.sql.*;  
  
public class MyListener implements ServletContextListener{  
public void contextInitialized(ServletContextEvent event) {  
try{  
Class.forName("oracle.jdbc.driver.OracleDriver");  
Connection con=DriverManager.getConnection(  
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");           

//storing connection object as an attribute in ServletContext  
ServletContext ctx=event.getServletContext();  
ctx.setAttribute("mycon", con);          

}catch(Exception e){e.printStackTrace();}  
}  

public void contextDestroyed(ServletContextEvent arg0) {}  
}  

MyListener.java

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

public class FetchData extends HttpServlet {        

public void doGet(HttpServletRequest request, HttpServletResponse response)  
        throws ServletException, IOException {    

response.setContentType("text/html");  
PrintWriter out = response.getWriter();  
      
try{  
//Retrieving connection object from ServletContext object  
ServletContext ctx=getServletContext();  
Connection con=(Connection)ctx.getAttribute("mycon");    

//retieving data  from emp32 table        
PreparedStatement ps=con.prepareStatement("select * from emp32",  
ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);                

ResultSet rs=ps.executeQuery();  
while(rs.next()){  
out.print("
"
+rs.getString(1)+" "+rs.getString(2)); } con.close(); }catch(Exception e){e.printStackTrace();} out.close(); } }

download this example (developed using Myeclipse IDE)

Example of ServletContextListener to create table of a project

We are creating a Table of the project in this example. So that we don't have to create all the tables manually in the database.

Mylistener.java

import javax.servlet.*;  
import java.sql.*;   

public class MyListener implements ServletContextListener{   

public void contextInitialized(ServletContextEvent arg0) {  
try{  
Class.forName("oracle.jdbc.driver.OracleDriver");  
Connection con=DriverManager.getConnection("  
jdbc:oracle:thin:@localhost:1521:xe","system","oracle");          

String query="create table emp32(id number(10),name varchar2(40))";  
PreparedStatement ps=con.prepareStatement(query);  
ps.executeUpdate();            

System.out.println(query);  
  
}catch(Exception e){e.printStackTrace();}  
}   

public void contextDestroyed(ServletContextEvent arg0) {  
System.out.println("project undeployed");  
          
}  
}  

download this example