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"); ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access