Java Reference
In-Depth Information
It takes a string that represents the key that was used to add the object in the
setAttribute()
method, and returns a reference to an object. You must downcast the object back to a
ConnectionPool
because when it was stored in the
ServletContext
, it was stored as an object.
The
destroy()
method then empties the
ConnectionPool
and removes the attribute from the
ServletContext
using the
removeAttribute()
method. The
destroy()
method is listed in the
following:
public void destroy() {
// Access the ServletContext using the getAttribute()
// method, which returns a reference to the ConnectionPool.
ServletContext context = getServletContext();
ConnectionPool pool =
(ConnectionPool)
context.getAttribute(“CONNECTION_POOL”);
if ( pool != null ) {
// empty the pool
pool.emptyPool();
// Remove the Attribute from the ServletContext
context.removeAttribute(“CONNECTION_POOL”);
}
else {
System.err.println(“Could not get a reference to Pool!”);
}
}
N
OTE
You might want to set up the
ConnectionPoolServlet
as a preloaded servlet. This will
make sure that the connections are already available before the first request is made.
You can do this by including the following lines in your
web.xml
file:
<servlet>
<servlet-name>ConnectionPool.ConnectionPoolServlet</servlet-name>
<servlet-class>ConnectionPool.ConnectionPoolServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>