Using an Application Scope Variable
One place for resources that
all components in
an application need access to is the application scope, corresponding
to ServletContext
attributes in the servlet world.
As I described in Chapter 19, the most appropriate
component for initialization and release of this type of shared
resources is the application lifecycle listener.
The container informs an application lifecycle listener when the
application is started and stopped. It can create the resource
objects and make them available to other application components in
its contextInitialized( )
method before any user
requests are received, and release them when the application is shut
down in its contextDestroyed( )
method. Finally, a
listener can use configuration data (defined as context parameters in
the deployment descriptor) to work in different settings. To recap,
here’s an application lifecycle listener similar to
the one used in Chapter 19:
package com.ora.jsp.servlets; import javax.servlet.*; import javax.servlet.http.*; import oracle.jdbc.pool.*; public class ResourceManagerListener2 implements ServletContextListener { private OracleConnectionCacheImpl ds = null; public void contextInitialized(ServletContextEvent sce) { ServletContext application = sce.getServletContext( ); String jdbcURL = application.getInitParameter("jdbcURL"); String user = application.getInitParameter("user"); String password = application.getInitParameter("password"); String maxLimit = application.getInitParameter("maxLimit"); ...
Get JavaServer Pages, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.