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 Example JSP Pages

To illustrate JSP technology, this chapter rewrites each servlet in the Duke’s Bookstoreapplication introduced inThe Example Servlets as a JSP page (seeTable 5-1).

Table 5-1 Duke’s Bookstore Example JSP Pages

Function

JSP Pages

Enter thebookstore.

bookstore.jsp

Create the bookstore banner.

banner.jsp

Browse the books offered for sale.

bookcatalog.jsp

Add a book tothe shopping cart.

bookcatalog.jsp andbookdetails.jsp

Get detailed information on a specific book.

bookdetails.jsp

Display theshopping cart.

bookshowcart.jsp

Remove one or more books from the shopping cart.

bookshowcart.jsp

Buy the booksin the shopping cart.

bookcashier.jsp

Receive an acknowledgment for the purchase.

bookreceipt.jsp

The data for the bookstore application is still maintained in a database andis accessed throughtut-install/javaeetutorial5/examples/web/bookstore2/src/java/com/sun/bookstore2/database/BookDBAO.java. However, the JSP pages accessBookDBAO through the JavaBeanscomponenttut-install/javaeetutorial5/examples/web/bookstore2/src/java/com/sun/bookstore2/database/BookDB.java. This class allows the JSP pages to use JSP elementsdesigned to work with JavaBeans components (seeJavaBeans Component Design Conventions).

The implementation of the database bean follows. The bean has two instance variables:the current book and the data access object.

package database;public class BookDB {    private String bookId = "0";    private BookDBAO database = null;    public BookDB () throws Exception {    }    public void setBookId(String bookId) {        this.bookId = bookId;    }    public void setDatabase(BookDAO database) {        this.database = database;    }    public Book getBook()         throws Exception {        return (Book)database.getBook(bookId);    }    ...}

This version of the Duke’s Bookstore application is organized along the Model-View-Controller (MVC)architecture. The MVC architecture is a widely used architectural approach for interactive applicationsthat distributes functionality among application objects so as to minimize the degree ofcoupling between the objects. To achieve this, it divides applications into three layers: model,view, and controller. Each layer handles specific tasks and has responsibilities to theother layers:

  • Themodel represents business data, along with business logic or operations that govern access and modification of this business data. The model notifies views when it changes and lets the view query the model about its state. It also lets the controller access application functionality encapsulated by the model. In the Duke’s Bookstore application, the shopping cart and database access object contain the business logic for the application.

  • Theview renders the contents of a model. It gets data from the model and specifies how that data should be presented. It updates data presentation when the model changes. A view also forwards user input to a controller. The Duke’s Bookstore JSP pages format the data stored in the session-scoped shopping cart and the page-scoped database bean.

  • Thecontroller defines application behavior. It dispatches user requests and selects views for presentation. It interprets user inputs and maps them into actions to be performed by the model. In a web application, user inputs are HTTPGET andPOST requests. A controller selects the next view to display based on the user interactions and the outcome of the model operations. In the Duke’s Bookstore application, theDispatcher servlet is the controller. It examines the request URL, creates and initializes a session-scoped JavaBeans component (the shopping cart), and dispatches requests to view JSP pages.


Note -When employed in a web application, the MVC architecture is often referred toas a Model-2 architecture. The bookstore example discussed inChapter 4, Java Servlet Technology, which intermixes presentationand business logic, follows what is known as a Model-1 architecture. The Model-2architecture is the recommended approach to designing web applications.


In addition, this version of the application uses several custom tags from theJavaServer Pages Standard Tag Library (JSTL), described inChapter 7, JavaServer Pages Standard Tag Library:

  • c:if,c:choose,c:when, andc:otherwise for flow control

  • c:set for setting scoped variables

  • c:url for encoding URLs

  • fmt:message,fmt:formatNumber, andfmt:formatDate for providing locale-sensitive messages, numbers, and dates

Custom tags are the preferred mechanism for performing a wide variety of dynamicprocessing tasks, including accessing databases, using enterprise services such as email and directories,and implementing flow control. In earlier versions of JSP technology, such tasks wereperformed with JavaBeans components in conjunction with scripting elements (discussed inChapter 9, Scripting in JSP Pages). Although stillavailable in JSP 2.0 technology, scripting elements tend to make JSP pages moredifficult to maintain because they mix presentation and logic, something that is discouragedin page design. Custom tags are introduced inUsing Custom Tags and described indetail inChapter 8, Custom Tags in JSP Pages.

Finally, this version of the example contains an applet to generate a dynamicdigital clock in the banner. SeeIncluding an Applet for a description of theJSP element that generates HTML for downloading the applet.

To deploy and run the application using NetBeans IDE, follow these steps:

  1. Perform all the operations described inAccessing Databases from Web Applications.

  2. In NetBeans IDE, select File→Open Project.

  3. In the Open Project dialog, navigate to:

    tut-install/javaeetutorial5/examples/web/
  4. Select thebookstore2 folder.

  5. Select the Open as Main Project check box and the Open Required Projects check box.

  6. Click Open Project.

  7. In the Projects tab, right-click thebookstore2 project, and select Undeploy and Deploy.

  8. To run the application, open the bookstore URLhttp://localhost:8080/bookstore2/books/bookstore.

To deploy and run the application using Ant, follow these steps:

  1. In a terminal window, go totut-install/javaeetutorial5/examples/web/bookstore2/.

  2. Typeant. This command will spawn any necessary compilations, copy files to thetut-install/javaeetutorial5/examples/web/bookstore2/build/ directory, and create a WAR file and copy it to thetut-install/javaeetutorial5/examples/web/bookstore2/dist/ directory.

  3. Start the Application Server.

  4. Perform all the operations described inCreating a Data Source in the Application Server.

  5. To deploy the example, typeant deploy. The deploy target outputs a URL for running the application. Ignore this URL, and instead use the one shown in the next step.

  6. To run the application, open the bookstore URLhttp://localhost:8080/bookstore2/books/bookstore.

To learn how to configure the example, refer to the deployment descriptor (theweb.xml file), which includes the following configurations:

  • Adisplay-name element that specifies the name that tools use to identify the application.

  • Acontext-param element that specifies the JSTL resource bundle base name.

  • Alistener element that identifies theContextListener class used to create and remove the database access.

  • Aservlet element that identifies theDispatcher servlet instance.

  • A set ofservlet-mapping elements that mapDispatcher to URL patterns for each of the JSP pages in the application.

  • Nested inside ajsp-config element are twojsp-property-group elements, which define the preludes and coda to be included in each page. SeeSetting Properties for Groups of JSP Pages for more information.

Figure 5-2 shows thebookcatalog.jsp page from the Duke’s Bookstore application. This page displaysa list of all the books that are available for purchase.

Figure 5-2 Book Catalog

Screen capture of Duke's Bookstore book catalog, with titles, authors, prices, and

SeeTroubleshooting Duke's Bookstore Database Problems for help with diagnosing common problems related to the database server.If the messages in your pages appear as strings of the form???Key???, the likely cause is that you have not provided thecorrect resource bundle base name as a context parameter.

PreviousContentsNext

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


[8]ページ先頭

©2009-2025 Movatter.jp