Java Reference
In-Depth Information
L ISTING 7.10 Continued
// 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!”);
7
}
}
//Get Servlet information
public String getServletInfo() {
return “ConnectionPoolServlet Information”;
}
}
The two areas of the ConnectionPoolServlet that you need to examine are the init() and
destroy() methods. The doGet() method is just a placeholder; it has no functionality.
The ConnectionPoolServlet.init() method creates the ConnectionPool and makes it avail-
able to the other servlets. It does this by first creating an instance of the ConnectionPool just
like any other application would. It then takes the newly created ConnectionPool object and
adds it to the shared ServletContext . It does this in the following code snippet:
// Once the pool is Initialized, add it to the
// Global ServletContext. This makes it available
// To other servlets using the same ServletContext.
ServletContext context = getServletContext();
context.setAttribute(“CONNECTION_POOL”, pool);
The preceding code gets a reference to the ServletContext , which is shared by all servlets
that exist in the same ServletContext . It then calls the setAttribute() method, passing it a
string that represents a key and a reference to the ConnectionPool object. This makes the
ConnectionPool available to other servlets.
The destroy() method does just the opposite. It first gets a reference to the ConnectionPool
by calling the ServletContext.getAttribute() method. The signature for this method is as
follows:
public java.lang.Object getAttribute(java.langString name)
 
Search WWH ::




Custom Search