0% found this document useful (0 votes)
30 views2 pages

Application Initialization Using Listeners

The document discusses using a ServletContextListener to initialize application components when a web application is deployed. Specifically, it provides an example of a ServletContextListener that creates a network of entity beans by loading data from an XML file when the application context is initialized. The listener's contextInitialized method gets called once before any servlets or filters are initialized, providing an opportunity to set up shared resources before the application handles any requests.

Uploaded by

akulaaksonen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

Application Initialization Using Listeners

The document discusses using a ServletContextListener to initialize application components when a web application is deployed. Specifically, it provides an example of a ServletContextListener that creates a network of entity beans by loading data from an XML file when the application context is initialized. The listener's contextInitialized method gets called once before any servlets or filters are initialized, providing an opportunity to set up shared resources before the application handles any requests.

Uploaded by

akulaaksonen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

APPLICATION INITIALIZATION USING LISTENERS

The June 26, 2003 Tech Tip, "Servlet Life Cycle Listeners" explained how to use servlet life cycle
event listeners to execute application-specific code at various points in a servlet life cycle. That tip also
mentioned that servlet life cycle event listeners can be used to initialize application components when
an application is deployed. The following tip presents an example of using a
ServletContextListener to create a network of entity beans when a Web application is deployed.

Servlet Life Cycle Listeners--Review

Life cycle listeners are event listener classes that receive events from a servlet container. The servlet
container notifies listeners when specific life cycle events occur. These events are:

 Creation or destruction of a servlet context or HTTP session


 Creation, modification, or removal of a servlet context attribute or an HTTP session attribute
 Activation or passivation of an HTTP session
 Notification to an object that has been bound to or unbound from an HTTP session attribute (in
versions that implement the servlet specification above 2.3)

Each Web application has a single ServletContext object. This object is shared between all servlets
and JSP pages in the application. (Distributed Web applications have one ServletContext for each
Web application, for each Java virtual machine*.) When a Web application is deployed, the Web
container initializes its ServletContext object. If the Web application's deployment descriptor
declares any ServletContextListeners, the container calls each listener's contextInitialized
method, in the order that the listeners are declared. This event occurs only once in the servlet's life
cycle: before any filter or servlet in the Web application is initialized. As a result, the method
ServletContextListener.contextInitialized can be used to initialize application components
when the application is deployed.

Note that Listeners must not use the interface javax.transaction.UserTransaction to demarcate
transactions.

Sample Code

The Tech Tip "Finder Methods and EJB-QL" follows this tip. It shows how to create finder methods to
identify collections of entity beans. The example used in that tip requires entity beans for the finder
methods to find. So there must be a way to create a network of entity beans when the application is
deployed. And that way is provided through a ServletContextListener. Specifically, the sample
code provided with the tip includes a class, LoadDataSCL, that implements a
ServletContextListener whose contextInitialized method creates a network of entity beans.
The method creates the beans based on the contents of an XML file that is deployed in the Web
application archive (WAR file). Here is the contextInitialized method of class LoadDataSCL:

public void contextInitialized(


ServletContextEvent sce) {
ServletContext sc = sce.getServletContext();
System.out.println(
"contextInitialized: Creating entity beans.");

DataLoader dl = new DataLoader();


InputStream is = sc.getResourceAsStream(
"/persondata.xml");
dl.load(is);

System.out.println(
"contextInitialized: Entity beans created.");
}

The method receives a single argument of type ServletContextEvent. This event contains a single
method, getServletContext, that returns a reference to the servlet context that is being initialized.
The DataLoader object creates local Person, Address, and PhoneNumber entity beans using data read
from an XML-formatted InputStream. The input stream comes from the call to
getResourceAsStream -- this opens a stream to an XML file packaged in the application archive.

When the application is deployed, the servlet container initializes its ServletContext object. It then
creates an instance of the LoadDataSCL class, and passes a ServletContextEvent object to its
contextInitialized method. The method then creates the network of entity beans defined in the
XML file. If you deploy the application on the Reference Implementation, it should produce the
following output in the server log file:

contextInitialized: Creating entity beans.


INFO: Document parsed
contextInitialized: Entity beans created.
Created Context:/ttaug2003
Application TTAug2003 deployed.

Note that the initialization occurs before the application deployment is complete. Using a listener to
initialize data ensures that the objects being created or initialized exist before any Web requests are
serviced.

This technique is not only useful for creating networks of entity beans. Any resource that might be
shared between Web components, such as database connections, JMS sessions, or Connector
references, could be initialized in a servlet context listener. References to the initialized or created
resources could be stored in context attributes for later use by servlets and JSP pages. Keep in mind
that it's important to synchronize access to Web components. That's because context attributes are
shared between Web components. Without proper synchronization, multiple servlet instances or JSP
pages might try simultaneously to access the shared resource. This would cause race conditions and
result in application failure.

Finally, you can use other types of listeners to create resources for Web applications. For example, you
might store a reference to a stateful session bean (representing a shopping cart, for instance) in an
HTTP session attribute. The same session bean reference could then be used to service multiple Web
requests in the same session. You could acquire this stateful session bean reference and store it in the
HTTP session using an HTTP session listener class. The class would need to properly handle session
migration and passivation/activation if the specific application server might perform these operations.

You might also like