Event Listening

A servlet can be designated as an event listener. This enables the servlet to be notified when some external event or change has occurred.

There are a number of listener interfaces that you can implement in your servlet. All the listener interfaces extend java.util.EventListener.

Table 12.6 provides a list of the listener interfaces.

Table 12.6. Servlet Event Listener Interfaces
Listener Interface Notification
HttpSessionActivationListener When a session is activated or passivated
HttpSessionAttributeListener When a session attribute is added, removed, or replaced
HttpSessionListener When a session is created or destroyed
ServletContextAttributeListener When a servlet context attribute is added, removed, or replaced
ServletContextListener About changes to servlet context, such as context initialization or the context is to be shut down

The listener classes are often used as a way of tracking sessions within a Web application. For example, it is often useful to know whether a session became invalid because the Web server timed out the session or because a Web component within the application called the invalidate() method.

In the filter section, we incremented a counter each time the application was accessed. The servlet filter code had to check that the counter existed before incrementing it. The following ServletContextListener (see Listing 12.11) sets up the counter when the context is initialized before any request is processed. The ServletContextListener interface has two methods:

  • contextInitialized() This receives notification that the application is ready to process requests.

  • contextDestroyed() This is notified that the context is about to be shut down.

Listing 12.11. Servlet Listener to Initialize Counter
 1: import javax.servlet.*;
 2: import javax.servlet.http.*;
 3:
 4: public class Listener extends HttpServlet implements ServletContextListener {
 5:
 6:    private ServletContext context = null;
 7:
 8:    public void contextInitialized(ServletContextEvent event) {
 9:       context = event.getServletContext();
10:
11:       Integer counter = new Integer(0);
12:       context.setAttribute("Counter", counter);
13:       context.log("Created Counter");
14:    }
15:
16:    public void contextDestroyed(ServletContextEvent event) {
17:       event.getServletContext().removeAttribute("Counter");
18:    }
19: }

Deploying the Listener

Go through the following steps to deploy the listener class in the Servlets application.

1.
Add a new Web component to the Servlets application. On the component page, check the Event Listeners box to indicate that this is an event listener class (see Figure 12.22). As with a filter, there is no need to give the listener servlet a component alias because it is not called directly.

Figure 12.22. deploytool Choose Component Type—Event Listeners.


2.
With the Servlets application highlighted in the left window, select the Event Listeners tab (see Figure 12.23).

Figure 12.23. deploytool Event Listeners.


3.
Add the Listener class to the event listeners (see Figure 12.24).

Figure 12.24. deploytool Event Listeners.


The following will then be added to the Servlets deployment descriptor:

<listener>
    <listener-class>Listener</listener-class>
  </listener>

4.
Deploy the application.

Because the listener initializes the counter to 0, and this is guaranteed to be called before any servlet code, you can now simplify the AuditFilter code and remove the check for a null counter (see Listing 12.12).

Listing 12.12. Amended AuditFilter Code Showing New doFilter() Method
 1: public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
 throws IOException, ServletException {
 2:     if (filterConfig == null)
 3:         return;
 4:     StringBuffer buf = new StringBuffer();
 5:     buf.append ("The number of hits is: ");
 6:     synchronized (this) {
 7:         Integer counter = (Integer)filterConfig.getServletContext().getAttribute
("Counter");
 8:         counter = new Integer(counter.intValue() + 1);
 9:         buf.append (counter.toString());
10:         filterConfig.getServletContext().log(buf.toString());
11:         filterConfig.getServletContext().setAttribute("Counter", counter);
12:     }
13:     chain.doFilter(req, res);
14: }
						

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.37.250