The Life Cycle of a JSP Page (original) (raw)
2. Using the Tutorial Examples
3. Getting Started with Web Applications
5. JavaServer Pages Technology
Using Objects within JSP Pages
Using Application-Specific Objects
Immediate and Deferred Evaluation Syntax
Deactivating Expression Evaluation
Process of Expression Evaluation
JavaBeans Component Design Conventions
Creating and Using a JavaBeans Component
Setting JavaBeans Component Properties
Retrieving JavaBeans Component Properties
Including the Tag Library Implementation
Transferring Control to Another Web Component
Setting Properties for Groups of JSP Pages
Deactivating EL Expression Evaluation
Further Information about JavaServer Pages Technology
7. JavaServer Pages Standard Tag Library
10. JavaServer Faces Technology
11. Using JavaServer Faces Technology in JSP Pages
12. Developing with JavaServer Faces Technology
13. Creating Custom UI Components
14. Configuring JavaServer Faces Applications
15. Internationalizing and Localizing Web Applications
16. Building Web Services with JAX-WS
17. Binding between XML Schema and Java Classes
19. SOAP with Attachments API for Java
21. Getting Started with Enterprise Beans
23. A Message-Driven Bean Example
24. Introduction to the Java Persistence API
25. Persistence in the Web Tier
26. Persistence in the EJB Tier
27. The Java Persistence Query Language
28. Introduction to Security in the Java EE Platform
29. Securing Java EE Applications
31. The Java Message Service API
32. Java EE Examples Using the JMS API
36. The Coffee Break Application
37. The Duke's Bank Application
The Life Cycle of a JSP Page
A JSP page services requests as a servlet. Thus, the life cycle and many of the capabilities of JSP pages (in particular the dynamic aspects) are determined by Java Servlet technology. You will notice that many sections in this chapter refer to classes and methods described in Chapter 4, Java Servlet Technology.
When a request is mapped to a JSP page, the web container first checks whether the JSP page’s servlet is older than the JSP page. If the servlet is older, the web container translates the JSP page into a servlet class and compiles the class. During development, one of the advantages of JSP pages over servlets is that the build process is performed automatically.
Translation and Compilation
During the translation phase each type of data in a JSP page is treated differently. Static data is transformed into code that will emit the data into the response stream. JSP elements are treated as follows:
- Directives are used to control how the web container translates and executes the JSP page.
- Scripting elements are inserted into the JSP page’s servlet class. See Chapter 9, Scripting in JSP Pages for details.
- Expression language expressions are passed as parameters to calls to the JSP expression evaluator.
- jsp:[set|get]Property elements are converted into method calls to JavaBeans components.
- jsp:[include|forward]elements are converted into invocations of the Java Servlet API.
- The jsp:plugin element is converted into browser-specific markup for activating an applet.
- Custom tags are converted into calls to the tag handler that implements the custom tag.
In the Application Server, the source for the servlet created from a JSP page named pageName is in this file:
domain-dir/generated/jsp/j2ee-modules/WAR-NAME/_pageName__jsp.java
For example, the source for the index page (named index.jsp) for the datelocalization example discussed at the beginning of the chapter would be named:
domain-dir/generated/jsp/j2ee-modules/date/index_jsp.java
Both the translation and the compilation phases can yield errors that are observed only when the page is requested for the first time. If an error is encountered during either phase, the server will return JasperException and a message that includes the name of the JSP page and the line where the error occurred.
After the page has been translated and compiled, the JSP page’s servlet (for the most part) follows the servlet life cycle described in Servlet Life Cycle:
- If an instance of the JSP page’s servlet does not exist, the container:
- Loads the JSP page’s servlet class
- Instantiates an instance of the servlet class
- Initializes the servlet instance by calling the jspInit method
- The container invokes the _jspService method, passing request and response objects.
If the container needs to remove the JSP page’s servlet, it calls thejspDestroy method.
Execution
You can control various JSP page execution parameters by using page directives. The directives that pertain to buffering output and handling errors are discussed here. Other directives are covered in the context of specific page-authoring tasks throughout the chapter.
Buffering
When a JSP page is executed, output written to the response object is automatically buffered. You can set the size of the buffer using the following page directive:
<%@ page buffer="none|_xxx_kb" %>
A larger buffer allows more content to be written before anything is actually sent back to the client, thus providing the JSP page with more time to set appropriate status codes and headers or to forward to another web resource. A smaller buffer decreases server memory load and allows the client to start receiving data more quickly.
Handling JSP Page Errors
Any number of exceptions can arise when a JSP page is executed. To specify that the web container should forward control to an error page if an exception occurs, include the following page directive at the beginning of your JSP page:
<%@ page errorPage="_file-name_" %>
The Duke’s Bookstore application page tut-install/javaeetutorial5/examples/web/bookstore2/web/template/preludeErrorPage.jspf contains the directive:
<%@ page errorPage="errorpage.jsp"%>
The following page directive at the beginning of tut-install/javaeetutorial5/examples/web/bookstore2/web/error/errorpage.jsp indicates that it is serving as an error page:
<%@ page isErrorPage="true" %>
This directive makes an object of type javax.servlet.jsp.ErrorData available to the error page so that you can retrieve, interpret, and possibly display information about the cause of the exception in the error page. You access the error data object in an EL (see Unified Expression Language) expression by way of the page context. Thus, pageContext.errorData.statusCoderetrievesthestatuscode,and{pageContext.errorData.statusCode} retrieves the status code, and pageContext.errorData.statusCoderetrievesthestatuscode,and{pageContext.errorData.throwable} retrieves the exception. You can retrieve the cause of the exception using this expression:
${pageContext.errorData.throwable.cause}
For example, the error page for Duke’s Bookstore is as follows:
<%@ page isErrorPage="true" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<fmt:message key="ServerError"/>: ${pageContext.errorData.throwable.cause}
Note - You can also define error pages for the WAR that contains a JSP page. If error pages are defined for both the WAR and a JSP page, the JSP page’s error page takes precedence.
Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Legal Notices