Home TOC |
![]() ![]() ![]() |
Initializing and Finalizing a JSP Page
You can customize the initialization process to allow the JSP page to read persistent configuration data, initialize resources, and perform any other one-time activities by overriding the
jspInit
method of theJspPage
interface. You release resources using thejspDestroy
method. The methods are defined using JSP declarations, discussed in Declarations.The bookstore example page
initdestroy.jsp
defines thejspInit
method to retrieve the objectdatabase.BookDBAO
that accesses the bookstore database and stores a reference to the bean inbookDBAO
.private BookDBAO bookDBAO; public void jspInit() { bookDBAO = (BookDBAO)getServletContext().getAttribute("bookDB"); if (bookDBAO == null) System.out.println("Couldn't get database."); }When the JSP page is removed from service, the
jspDestroy
method releases theBookDBAO
variable.public void jspDestroy() { bookDBAO = null; }Since the enterprise bean is shared between all the JSP pages, it should be initialized when the application is started, instead of in each JSP page. Java Servlet technology provides application life cycle events and listener classes for this purpose. As an exercise, you can move the code that manages the creation of the enterprise bean to a context listener class. See Handling Servlet Life Cycle Events for the context listener that initializes the Java Servlet version of the bookstore application.
Home TOC |
![]() ![]() ![]() |