|
Home TOC |
|
Initializing a Servlet
After the Web container loads and instantiates the servlet class and before it delivers requests from clients, the Web container initializes the servlet. You can customize this process to allow the servlet to read persistent configuration data, initialize resources, and perform any other one-time activities by overriding the
initmethod of theServletinterface. A servlet that cannot complete its initialization process should throwUnavailableException.All the servlets that access the bookstore database (
BookStoreServlet,CatalogServlet,BookDetailsServlet, andShowCartServlet)initialize a variable in theirinitmethod that points to the database helper object created by the Web context listener:public class CatalogServlet extends HttpServlet { private BookDB bookDB; public void init() throws ServletException { bookDB = (BookDB)getServletContext(). getAttribute("bookDB"); if (bookDB == null) throw new UnavailableException("Couldn't get database."); } }
|
Home TOC |
|