Listing 6.40 shows a JSP page that prints the current order count. Figures 6??“20
through 6??“22 show some typical results.
Listing 6.37 DailySpecialRegistrar.java
package coreservlets.listeners;
import java.util.*;
import javax.servlet.*;
/** Listener that records how to detect orders
* of the daily special. It reads a list of attribute
* names from an init parameter: these correspond to
* session attributes that are used to record orders.
* It also reads a list of item names: these correspond
* to the names of the daily specials. Other listeners
* will watch to see if any daily special names appear
* as values of attributes that are hereby designated
* to refer to orders.
*/
6.15 Using Multiple Cooperating Listeners 327
public class DailySpecialRegistrar
implements ServletContextListener {
/** When the Web application is loaded, record the
* attribute names that correspond to orders and
* the attribute values that are the daily specials.
* Also set to zero the count of daily specials that have
* been ordered.
*/
public void contextInitialized(ServletContextEvent event) {
ServletContext context = event.getServletContext();
addContextEntry(context, "order-attribute-names");
addContextEntry(context, "daily-special-item-names");
context.setAttribute("dailySpecialCount", new Integer(0));
}
public void contextDestroyed(ServletContextEvent event) {}
/** Read the designated context initialization parameter,
* put the values into an ArrayList, and store the
* list in the ServletContext with an attribute name
* that is identical to the initialization parameter name.
Pages:
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381