The J2EETM Tutorial
Home
TOC
PREV TOC NEXT Search
Feedback

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 or create a JavaBeans component database.BookDB that represents the bookstore database:

private BookDB bookDB;
public void jspInit() {
	bookDB =
	(BookDB)getServletContext().
		getAttribute("bookDB");

	if (bookDB == null) {
		try {
			bookDB = new BookDB();
			getServletContext().setAttribute(
				"bookDB", bookDB);
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}
} 

and the jspDestroy method to remove the JavaBeans component and release the BookDB variable.

public void jspDestroy() {
	try {
		bookDB.remove();
	} catch (Exception ex) {
		System.out.println(ex.getMessage());
	}
	getServletContext().removeAttribute("bookDB");
	bookDB = null;
} 

The implementation of the database bean follows. The bean has two instance variables: the current book and a reference to the database enterprise bean. The reference is created using the techniques described in Getting Started.

public class BookDB {
	String bookId = "0";
	private BookDBEJB database = null;

	public BookDB () throws Exception {
		try {
			InitialContext ic = new InitialContext();
			Object objRef = ic.lookup(
				"java:comp/env/ejb/BookDBEJB");
			BookDBEJBHome home =
				(BookDBEJBHome)PortableRemoteObject.
					narrow(objRef, 
					database.BookDBEJBHome.class);
			database = home.create();
		} catch (RemoteException ex) {
			throw new Exception(
				"Couldn't create database bean.");
		} catch (CreateException ex) {
			throw new Exception(
				"Couldn't create database bean.");
		} catch (NamingException ex) {
			throw new Exception("Unable to lookup home: "+
			"java:comp/env/ejb/BookDBEJB.");
	}
	public void remove () throws Exception {
		try {
			database.remove();
			database = null;
		} catch (RemoteException ex) {
			throw new Exception(
				"Error while removing database bean.");
		} catch (EJBException ex) {
			throw new Exception(
				"Error while removing database bean.");
		} catch (RemoveException ex) {
			throw new Exception(
				"Error while removing database bean.");
		}
	}
	public void setBookId(String bookId) {
		this.bookId = bookId;
	}
	...
} 

Since the database 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 bean to a context listener class. See Monitoring Servlet Life Cycle Events for the context listener that initializes the Java Servlet version of the bookstore application.

Home
TOC
PREV TOC NEXT Search
Feedback