FAQ
History
PreviousHomeNext Search
Feedback
Divider

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 the JspPage interface. You release resources using the jspDestroy method. The methods are defined using JSP declarations, discussed in Declarations.

The bookstore example page initdestroy.jsp defines the jspInit method to retrieve the object database.BookDBAO that accesses the bookstore database and stores a reference to the bean in bookDBAO.

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 the BookDBAO 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.

Divider
FAQ
History
PreviousHomeNext Search
Feedback
Divider

All of the material in The J2EE Tutorial for the Sun ONE Platform is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.