📄 jspintro4.html
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>The Life Cycle of a JSP Page</title> <link rel="StyleSheet" href="document.css" type="text/css" media="all" /> <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" /> <link rel="Table of Contents" href="J2EETutorialTOC.html" /> <link rel="Previous" href="JSPIntro3.html" /> <link rel="Next" href="JSPIntro5.html" /> <link rel="Index" href="J2EETutorialIX.html" /> </head> <body> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="JSPIntro3.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="JSPIntro5.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <blockquote><a name="wp65950"> </a><h2 class="pHeading1">The Life Cycle of a JSP Page</h2><a name="wp65952"> </a><p class="pBody">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 <a href="Servlets.html#wp69954">11</a>.</p><a name="wp65956"> </a><p class="pBody">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.</p><a name="wp65959"> </a><h3 class="pHeading2">Translation and Compilation</h3><a name="wp65961"> </a><p class="pBody">During the translation phase each type of data in a JSP page is treated differently. Template data is transformed into code that will emit the data into the response stream. JSP elements are treated as follows:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp65964"> </a><div class="pSmartList1"><li>Directives are used to control how the Web container translates and executes the JSP page.</li></div><a name="wp65967"> </a><div class="pSmartList1"><li>Scripting elements are inserted into the JSP page's servlet class. See Chapter <a href="JSPAdvanced.html#wp65706">16</a> for details.</li></div><a name="wp76658"> </a><div class="pSmartList1"><li>Expression language expressions are passed as parameters to calls to the JSP expression evaluator.</li></div><a name="wp65969"> </a><div class="pSmartList1"><li><code class="cCode">jsp:[set|get]Property</code> elements are converted into method calls to JavaBeans components.</li></div><a name="wp84028"> </a><div class="pSmartList1"><li><code class="cCode">jsp:[include|forward</code>] elements are converted to invocations of the Java Servlet API.</li></div><a name="wp84116"> </a><div class="pSmartList1"><li>The <code class="cCode">jsp:plugin</code> element is converted to browser-specific markup for activating an applet.</li></div><a name="wp82685"> </a><div class="pSmartList1"><li>Custom tags are converted into calls to the tag handler that implements the custom tag.</li></div></ul></div><a name="wp97052"> </a><p class="pBody">If you would like the J2EE 1.4 Application Server to keep the generated servlets, for a Web module in deploytool, </p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp97053"> </a><div class="pSmartList1"><li>Select the WAR.</li></div><a name="wp97054"> </a><div class="pSmartList1"><li>Select the General tab.</li></div><a name="wp97055"> </a><div class="pSmartList1"><li>Click the Sun-specific Setting button.</li></div><a name="wp97056"> </a><div class="pSmartList1"><li>Select the JSP option from the View combo box.</li></div><a name="wp97057"> </a><div class="pSmartList1"><li>Click the Add button in the JSP Configuration frame.</li></div><a name="wp97058"> </a><div class="pSmartList1"><li>Select keepgenerated from the Name column.</li></div><a name="wp97059"> </a><div class="pSmartList1"><li>Select true from the Value column.</li></div><a name="wp97060"> </a><div class="pSmartList1"><li>Click Close.</li></div></ol></div><a name="wp94837"> </a><p class="pBody">In the J2EE application server, the source for the servlet created from a JSP page named <code class="cVariable">pageName</code> is in the file:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cVariable"><J2EE_HOME></code>/domains/domain1/generated/ jsp/<code class="cVariable">WAR_NAME</code>/<code class="cVariable">pageName</code>_jsp.java<a name="wp97083"> </a></pre></div><a name="wp65972"> </a><p class="pBody">For example, the source for the index page (named <code class="cCode">index.jsp</code>) for the <code class="cCode">date</code> localization example discussed at the beginning of the chapter would be named:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cVariable"><J2EE_HOME></code>/domains/domain1/generated/ jsp/<code class="cCode">date/index</code>_jsp.java<a name="wp97101"> </a></pre></div><a name="wp91810"> </a><p class="pBody">Both the translation and compilation phases can yield errors that are only observed when the page is requested for the first time. If an error is encountered during either phase, the server will return <code class="cCode">JasperException</code> and a message that includes the name of the JSP page and the line where the error occurred. </p><a name="wp65978"> </a><p class="pBody">Once the page has been translated and compiled, the JSP page's servlet for the most part follows the servlet life cycle described in <a href="Servlets4.html#wp73579">Servlet Life Cycle</a>:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp65982"> </a><div class="pSmartList1"><li>If an instance of the JSP page's servlet does not exist, the container</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp65983"> </a><div class="pSmartList2"><li>Loads the JSP page's servlet class</li></div><a name="wp65984"> </a><div class="pSmartList2"><li>Instantiates an instance of the servlet class</li></div><a name="wp65985"> </a><div class="pSmartList2"><li>Initializes the servlet instance by calling the <code class="cCode">jspInit</code> method</li></div></ol></div><a name="wp65986"> </a><div class="pSmartList1"><li>The container invokes the <code class="cCode">_jspService</code> method, passing a request and response object.</li></div></ol></div><a name="wp65987"> </a><p class="pBody">If the container needs to remove the JSP page's servlet, it calls the <code class="cCode">jspDestroy</code> method.</p><a name="wp65988"> </a><h3 class="pHeading2">Execution</h3><a name="wp65990"> </a><p class="pBody">You can control various JSP page execution parameters by using <code class="cCode">page</code> 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.</p><a name="wp65991"> </a><h4 class="pHeading3">Buffering</h4><a name="wp65992"> </a><p class="pBody">When a JSP page is executed, output written to the response object is automatically buffered. You can set the size of the buffer with the following page directive:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><%@ page buffer="none|<code class="cVariable">xxx</code>kb" %><a name="wp65994"> </a></pre></div><a name="wp65995"> </a><p class="pBody">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.</p><a name="wp65997"> </a><h4 class="pHeading3">Handling Errors</h4><a name="wp65998"> </a><p class="pBody">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 <code class="cCode">page</code> directive at the beginning of your JSP page:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><%@ page errorPage="<code class="cVariable">file_name</code>" %><a name="wp82208"> </a></pre></div><a name="wp82210"> </a><p class="pBody">The Duke's Bookstore application page <code class="cCode"><a href="../examples/web/bookstore2/web/template/prelude.txt" target="_blank">prelude.jsp</a></code> contains the directive</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><%@ page errorPage="errorpage.jsp"%><a name="wp82211"> </a></pre></div><a name="wp66006"> </a><p class="pBody">The beginning of <code class="cCode"><a href="../examples/web/bookstore2/web/template/errorpage.txt" target="_blank">errorpage.jsp</a></code> indicates that it is serving as an error page with the following <code class="cCode">page</code> directive:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><%@ page isErrorPage="true" %><a name="wp66007"> </a></pre></div><a name="wp76968"> </a><p class="pBody">This directive makes an object of type <code class="cCode"><a href="http://java.sun.com/j2ee/1.4/docs/api/index.htmlapi/javax/servlet/jsp/ErrorData.html" target="_blank">javax.servlet.jsp.ErrorData</a></code> 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 expression via the page context. Thus, <code class="cCode">${pageContext.errorData.statusCode}</code> is used to retrieve the status code and <code class="cCode">${pageContext.errorData.throwable}</code> retrieves the exception. If the exception is generated during the evaluation of an EL expression, you can retrieve the root cause of the exception with the expression ${pageContext.errorData.throwable.rootCause}. For example, the error page for the Duke's Bookstore is:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><%@ 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" %><html><head><title><fmt:message key="ServerError"/></title></head><body bgcolor="white"><h3><fmt:message key="ServerError"/></h3><p>${pageContext.errorData.throwable}<c:choose> <c:when test="${!empty pageContext.errorData.throwable.cause}"> : ${pageContext.errorData.throwable.cause} </c:when> <c:when test="${!empty pageContext.errorData.throwable.rootCause}"> : ${pageContext.errorData.throwable.rootCause} </c:when></c:choose></body></html><a name="wp83785"> </a></pre></div><hr><a name="wp73170"> </a><p class="pNote">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.</p><hr> </blockquote> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="JSPIntro3.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="JSPIntro5.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -