Event and Listener
Event and Listener
Events
• Events are basically occurrence of something. Changing the state of an
object is known as an event.
• We can perform some important tasks at the occurrence of these exceptions,
such as counting total and current logged-in users, creating tables of the
database at time of deploying the project, creating database connection
object etc.
Event classes
The event classes are as follows:
• ServletRequestEvent
• ServletContextEvent
• ServletRequestAttributeEvent
• ServletContextAttributeEvent
• HttpSessionEvent
• HttpSessionBindingEvent
Event Listener
• The event interfaces are as follows:
• ServletRequestListener
• ServletRequestAttributeListener
• ServletContextListener
• ServletContextAttributeListener
• HttpSessionListener
• HttpSessionAttributeListener
• HttpSessionBindingListener
• HttpSessionActivationListener
ServletContextEvent and
ServletContextListener
• The ServletContextEvent is notified when web application is deployed on
the server.
•
If you want to perform some action at the time of deploying the web
application such as creating database connection, creating all the tables of
the project etc, you need to implement ServletContextListener interface and
provide the implementation of its methods.
ServletContextEvent and
ServletContextListener
• Constructor of ServletContextEvent class
• There is only one constructor defined in the ServletContextEvent class. The
web container creates the instance of ServletContextEvent after the
ServletContext instance.
• ServletContextEvent(ServletContext e)
ServletContextEvent and
ServletContextListener
• Method of ServletContextEvent class
• There is only one method defined in the ServletContextEvent class:
• public ServletContext getServletContext(): returns the instance of
ServletContext.
ServletContextEvent and
ServletContextListener
• Methods of ServletContextListener interface
• There are two methods declared in the ServletContextListener interface
which must be implemented by the servlet programmer to perform some
action such as creating database connection etc.
• public void contextInitialized(ServletContextEvent e): is invoked when
application is deployed on the server.
• public void contextDestroyed(ServletContextEvent e): is invoked when
application is undeployed from the server.
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();} }
response.setContentType("text/html"); con.close();
PrintWriter out = response.getWriter(); }catch(Exception e){e.printStackTrace();}
try{ out.close();
//Retrieving connection object from ServletContext object }
ServletContext ctx=getServletContext(); }
Connection con=(Connection)ctx.getAttribute("mycon");