[ Team LiB ] Previous Section Next Section

JavaServer Page Lifecycle

A JSP has an extra phase in its lifecycle, because it must also be compiled. The phases of a JSP's lifecycle are

  1. Compilation

  2. Loading

  3. Initialization

  4. Execution

  5. Cleanup

JSP Compilation

When a browser asks for a JSP, the JSP engine first checks to see whether it needs to compile the page. If the page has never been compiled, or if the JSP has been modified since it was last compiled, the JSP engine compiles the page. The compilation process involves first parsing the JSP, turning the JSP into a servlet, and then compiling the servlet.

You can circumvent the compilation process by manually generating a servlet for the JSP and compiling it by hand. In Hour 24, "Performance," you'll learn how to do this.

Changing JSP Source After Hand Compilation


If you compile the JSP by hand but then change the JSP source file later, the JSP engine will recognize that the JSP must be recompiled. If you want to control the compilation totally, you must deploy your application with a Web Archive file (WAR), which is discussed in detail in Appendix B, "Packaging a Web Application." You can also set up a URL mapping that forces YourPageName.jsp to run a specific servlet, avoiding the JSP compiler altogether.

JSP Loading

Because a JSP becomes a servlet before it is actually loaded, the loading process for a JSP is similar to the loading process for a servlet. The main difference between a JSP and a servlet is that the class file for the JSP may be reloaded, if a newer version of a JSP source file is found.

Reloading Associated Classes


Although a JSP class may be reloaded, any associated classes, such as JavaBeans or utility classes, are not always reloaded automatically. Some JSP engines might automatically reload other classes, but don't count on it.

JSP Initialization

Although a JSP is compiled into a servlet, JSP developers should not override the underlying servlet init and destroy methods. If you need to perform JSP-specific initialization, override the jspInit method:

public void jspInit()

As with the servlet init method, you generally initialize database connections, open files, and create lookup tables in the jspInit method.

You might wonder if there's something similar to the ServletConfig object available to a servlet. After all, it does seem pretty useful to be able to access initialization parameters or the Web application context. In fact, there are a few implicit objects available to you: config is a reference to the ServletConfig object for the JSP, and application is a reference to the ServletContext object.

Only one problem remains: How do you associate initialization parameters with a JSP? From Hour 2, recall that a servlet has an entry in a deployment descriptor that looks like this:


Any properties that are specific to a servlet will be defined as part of the servlet element. So far, we've only named the servlet and identified the class file. Earlier in this chapter you saw how to associate an initialization parameter with a servlet by adding an init-param element.

Fortunately, you don't have to try and guess what the name of the class that is produced by compiling a JSP will be. There's an alternate form especially for JSPs:


You use this exactly as you would a servlet declaration; that is, you can add an init-param element.

JSP Execution

Whenever a browser requests a JSP and the page has been loaded and initialized, the JSP engine invokes the jspService method in the JSP. The jspService method takes an HttpServletRequest and an HttpServletResponse as its parameters.

Never Override the jspService Method


Because the JSP compiler generates the jspService method, you should never try to override this method yourself.

JSP Cleanup

The jspDestroy method is the JSP equivalent of the destroy method for servlets. Override jspDestroy when you need to perform any cleanup, such as releasing database connections or closing open files. The jspDestroy method is declared this way:

    public void jspDestroy()
        // Your cleanup code goes here.
    [ Team LiB ] Previous Section Next Section