Recipe7.1.Performing Tasks at Application Startup


Recipe 7.1. Performing Tasks at Application Startup

Problem

You want to be notified when your web application is initialized so you can preload application-scope data or execute other startup functions.

Solution

Create a class that implements the ServletContextListener interface. The class shown in Example 7-1 stores the current date and time in the servlet context when the application is started.

Example 7-1. Servlet context data loader
package com.oreilly.strutsckbk.ch07; import java.util.Date; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class ContextLoader implements ServletContextListener {     public void contextInitialized(ServletContextEvent event) {         ServletContext ctx = event.getServletContext( );         ctx.setAttribute("dateStarted", new Date( ));     }     public void contextDestroyed(ServletContextEvent event) {         // clean up here     } }

Declare the class with a listener element in your web application's web.xml file. The listener element is supported by Version 2.3 or 2.4 of the Servlet specification; the DTD must specify Version 2.3 or 2.4:

<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE web-app PUBLIC     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"     "http://java.sun.com/dtd/web-app_2_3.dtd">      <web-app>   <display-name>Struts Cookbook - Chapter 7 Examples</display-name>      <listener>       <listener-class>com.oreilly.strutsckbk.ch07.ContextLoader       </listener-class>   </listener>   ... rest of web.xml

Discussion

Every Java web application has one single servlet context. This context is the place where application-scoped attributes are stored. The servlet container creates the servlet context for a web application when the application is started. You can create a class that gets notified when this context is created (or destroyed) by implementing the ServletContextListener interface. Your listener class receives notifications immediately after the servlet context is created and immediately before the context is destroyed. These events coincide with the startup and shutdown of the web application.

The ServletContextListener interface defines two methods: contextInitialized( ) and contextDestroyed( ). The container passes a ServletContextEvent to these methods. This class provides access to the ServletContext itself. With the servlet context, you can add, remove, and replace application-scope objects as servlet context attributes. The class in Example 7-1 uses the ServletContext.setAttribute(name,value) method to store the current a Date object in the servlet context. You could access this data from a JSP page as an application-scoped object:

Running since: <bean:write name="dateStarted"                           scope="application"                          format="MM/dd/yy"/>

Servlet context listeners make an excellent choice for this kind of data loading, but for a Struts application you may prefer to use a Struts plug-in. A plug-in can do anything a context listener can do, and a plug-in gives you access to your entire application's Struts configuration through the ActionServlet. You can pass parameters to a plug-in using the set-property element, making plug-ins more flexible and reusable than listeners. Because you declare plug-ins in the struts-config.xml file, you get a side benefit as your application's configuration will be centralized in one place.

See Also

Recipe 2.1 gives complete details on writing and configuring Struts plug-ins. Recipe 7.2 describes how to create classes that monitor HTTP sessions.

If you are unfamiliar with servlet programming, then you should check out Java Servlet Programming by Jason Hunter (O'Reilly). This classic work will give you all the information you need to create highly functional servlets and servlet-related classes.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net