[ Team LiB ] |
![]() ![]() |
JavaServer Page LifecycleA JSP has an extra phase in its lifecycle, because it must also be compiled. The phases of a JSP's lifecycle are
JSP CompilationWhen 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
JSP LoadingBecause 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
JSP InitializationAlthough 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: <servlet> <servlet-name>HelloWorldServlet</servlet-name> <servlet-class>examples.HelloWorldServlet</servlet-class> ... </servlet> 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: <servlet> <servlet-name>HelloWorldJSP</servlet-name> <jsp-file>/HelloWorld.jsp</jsp-file> ... </servlet> You use this exactly as you would a servlet declaration; that is, you can add an init-param element. JSP ExecutionWhenever 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
JSP CleanupThe 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 ] |
![]() ![]() |