Movatterモバイル変換


[0]ホーム

URL:


Document Information

Preface

Part I Introduction

1.  Overview

2.  Using the Tutorial Examples

Part II The Web Tier

3.  Getting Started with Web Applications

4.  Java Servlet Technology

5.  JavaServer Pages Technology

What Is a JSP Page?

A Simple JSP Page Example

The Example JSP Pages

The Life Cycle of a JSP Page

Translation and Compilation

Execution

Buffering

Handling JSP Page Errors

Creating Static Content

Response and Page Encoding

Creating Dynamic Content

Using Objects within JSP Pages

Using Implicit Objects

Using Application-Specific Objects

Using Shared Objects

Unified Expression Language

Immediate and Deferred Evaluation Syntax

Immediate Evaluation

Deferred Evaluation

Value and Method Expressions

Value Expressions

Method Expressions

Defining a Tag Attribute Type

Deactivating Expression Evaluation

Literal Expressions

Resolving Expressions

Process of Expression Evaluation

EL Resolvers

Implicit Objects

Operators

Reserved Words

Examples of EL Expressions

Functions

Using Functions

Defining Functions

JavaBeans Components

JavaBeans Component Design Conventions

Creating and Using a JavaBeans Component

Setting JavaBeans Component Properties

Retrieving JavaBeans Component Properties

Using Custom Tags

Declaring Tag Libraries

Including the Tag Library Implementation

Reusing Content in JSP Pages

Transferring Control to Another Web Component

jsp:param Element

Including an Applet

Setting Properties for Groups of JSP Pages

Deactivating EL Expression Evaluation

Declaring Page Encodings

Defining Implicit Includes

Eliminating Extra White Space

Further Information about JavaServer Pages Technology

6.  JavaServer Pages Documents

7.  JavaServer Pages Standard Tag Library

8.  Custom Tags in JSP Pages

9.  Scripting in JSP Pages

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

Part III Web Services

16.  Building Web Services with JAX-WS

17.  Binding between XML Schema and Java Classes

18.  Streaming API for XML

19.  SOAP with Attachments API for Java

Part IV Enterprise Beans

20.  Enterprise Beans

21.  Getting Started with Enterprise Beans

22.  Session Bean Examples

23.  A Message-Driven Bean Example

Part V Persistence

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

Part VI Services

28.  Introduction to Security in the Java EE Platform

29.  Securing Java EE Applications

30.  Securing Web Applications

31.  The Java Message Service API

32.  Java EE Examples Using the JMS API

33.  Transactions

34.  Resource Connections

35.  Connector Architecture

Part VII Case Studies

36.  The Coffee Break Application

37.  The Duke's Bank Application

Part VIII Appendixes

A.  Java Encoding Schemes

B.  About the Authors

Index

 

The Java EE 5 Tutorial

Java Coffee Cup logo
PreviousContentsNext

The Life Cycle of a JSP Page

A JSP page services requests as a servlet. Thus, the life cycle andmany of the capabilities of JSP pages (in particular the dynamic aspects) aredetermined by Java Servlet technology. You will notice that many sections in thischapter refer to classes and methods described inChapter 4, Java Servlet Technology.

When a request is mapped to a JSP page, the web container firstchecks whether the JSP page’s servlet is older than the JSP page. Ifthe servlet is older, the web container translates the JSP page into aservlet class and compiles the class. During development, one of the advantages ofJSP 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 istreated differently. Static data is transformed into code that will emit the datainto 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. SeeChapter 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.

  • Thejsp: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 aJSP page namedpageName is in this file:

domain-dir/generated/jsp/j2ee-modules/WAR-NAME/pageName_jsp.java

For example, the source for the index page (namedindex.jsp) for thedatelocalization 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 observedonly when the page is requested for the first time. If an erroris encountered during either phase, the server will returnJasperException and a messagethat includes the name of the JSP page and the line where theerror occurred.

After the page has been translated and compiled, the JSP page’s servlet (forthe most part) follows the servlet life cycle described inServlet Life Cycle:

  1. If an instance of the JSP page’s servlet does not exist, the container:

    1. Loads the JSP page’s servlet class

    2. Instantiates an instance of the servlet class

    3. Initializes the servlet instance by calling thejspInit method

  2. 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 usingpage directives. Thedirectives that pertain to buffering output and handling errors are discussed here. Otherdirectives 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 isautomatically buffered. You can set the size of the buffer using thefollowing page directive:

<%@ page buffer="none|xxxkb" %>

A larger buffer allows more content to be written before anything is actuallysent back to the client, thus providing the JSP page with moretime to set appropriate status codes and headers or to forward to anotherweb resource. A smaller buffer decreases server memory load and allows the clientto start receiving data more quickly.

Handling JSP Page Errors

Any number of exceptions can arise when a JSP page is executed. Tospecify that the web container should forward control to an error page ifan exception occurs, include the followingpage directive at the beginning ofyour JSP page:

<%@ page errorPage="file-name" %>

The Duke’s Bookstore application pagetut-install/javaeetutorial5/examples/web/bookstore2/web/template/preludeErrorPage.jspf contains the directive:

<%@ page errorPage="errorpage.jsp"%>

The following page directive at the beginning oftut-install/javaeetutorial5/examples/web/bookstore2/web/error/errorpage.jsp indicates that it isserving as an error page:

<%@ page isErrorPage="true" %>

This directive makes an object of typejavax.servlet.jsp.ErrorData available to the errorpage so that you can retrieve, interpret, and possibly display information about thecause of the exception in the error page. You access the error dataobject in an EL (seeUnified Expression Language) expression by way of the pagecontext. Thus,${pageContext.errorData.statusCode} retrieves the status code, 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" %><html><head><title><fmt:message key="ServerError"/></title></head><body bgcolor="white"><h3><fmt:message key="ServerError"/></h3><p>: ${pageContext.errorData.throwable.cause}</body></html>

Note -You can also define error pages for the WAR that contains aJSP page. If error pages are defined for both the WAR and aJSP page, the JSP page’s error page takes precedence.


PreviousContentsNext

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices


[8]ページ先頭

©2009-2025 Movatter.jp