Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

This will help in learning web development through servlets and JSP

Authors

Getting Started with Web applications development with servlets and JSP

Getting Started with Web applications development with servlets and JSP - Part 4: Events

May 10 2012 12:00AM by Vladimir Djurovic   

This installment of tutorial will deal with events and events listeners in servlet container.

Each servlet is started and executed within a servlet context, which represents an environment in which servlet is being executed. It also allows servlets to access servlet container (i.e. Tomcat), and get information about its environment. During its lifecycle, servlet context publishes events, which can be captured by registered listeners in order to perform certain actions.

For example, application might want to establish a database connection upon startup, so it registers listeners, which are notified when servlet context is being started. These listeners will then perform required actions to connect to database.

Per servlet specification, servlet context publishes two types of events:

  • Context initialized – when servlet context is being initialized on startup
  • Context destroyed – when context is being shut down

A ServletContextListener handles both of these events. Applications will extend this class to provide custom event handling procedure. Here is a sample code:

public class SampleContextListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Context initialized.");
    }

    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("Context destroyed");
    }    
}

This sample only prints out information to standard out when context is initialized or destroyed. In order for listener to work, it must be registered in deployment descriptor. Add the following code snippet to web.xml:

<listener>
      <listener-class>com.beyondrelational.SampleContextListener</listener-class>
  </listener>

Now, when you run the server, and then shut it down, you should see the following output:

Context initialization and destroy

Notice that messages are printed when context is started, and again when server is shutdown, i.e. when context is destroyed.

Servlet specification defines several more types of event listeners, which will be covered later in this tutorial. These are the following:

  • Session lifecycle listeners – handle session lifecycle events, i.e. when session is created, invalidated, or timeout
  • Session attribute listeners – handle changes to session attributes
  • Servlet context attribute listeners – handle changes to context attributes

Finally, there are a few key points that you need to keep in mind when working with listeners:

  • Servlet applications are usually multithreaded, so listeners must take care of data synchronization and integrity
  • Each listener must have public zero-argument constructor
  • Each listener must be packaged in .war file, under WEB-INF/classes, or WEB-INF/lib

Vladimir Djurovic
78 · 2% · 717
5



Submit

Your Comment


Sign Up or Login to post a comment.

"Getting Started with Web applications development with servlets and JSP - Part 4: Events" rated 5 out of 5 by 5 readers
Getting Started with Web applications development with servlets and JSP - Part 4: Events , 5.0 out of 5 based on 5 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]