Document Information


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

6.  JavaServer Pages Documents

7.  JavaServer Pages Standard Tag Library

8.  Custom Tags in JSP Pages

What Is a Custom Tag?

Types of Tags

Tags with Attributes

Simple Attributes

Fragment Attributes

Dynamic Attributes

Deferred Value

Deferred Method

Dynamic Attribute or Deferred Expression

jsp:attribute Element

Tags with Bodies

jsp:body Element

Tags That Define Variables

Communication between Tags

Encapsulating Reusable Content Using Tag Files

Tag File Location

Tag File Directives

Declaring Tags

body-content Attribute

Declaring Tag Attributes in Tag Files

Declaring Tag Variables in Tag Files

Evaluating Fragments Passed to Tag Files

Custom Tag Examples

Simple Attribute Example

Simple and Fragment Attribute and Variable Example

Dynamic Attribute Example

Tag Library Descriptors

Top-Level Tag Library Descriptor Elements

validator Element

listener Element

Declaring Tag Files

tag-file TLD Element

Unpackaged Tag Files

Packaged Tag Files

Declaring Tag Handlers

body-content Element

Declaring Tag Attributes for Tag Handlers

Declaring Tag Variables for Tag Handlers

Programming Simple Tag Handlers

Including Tag Handlers in Web Applications

How Is a Simple Tag Handler Invoked?

Tag Handlers for Basic Tags

Tag Handlers for Tags with Attributes

Defining Attributes in a Tag Handler

Attribute Validation

Setting Dynamic Attributes

Setting Deferred Value Attributes and Deferred Method Attributes

Tag Handlers for Tags with Bodies

Tag Handler Does Not Manipulate the Body

Tag Handlers for Tags That Define Variables

TagExtraInfo Class

Cooperating Tags

Tag Handler Examples

An Iteration Tag

A Template Tag Library

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



The Example JSP Pages

This chapter describes the tasks involved in defining simple tags. It illustrates the tasks using excerpts from the JSP version of the Duke’s Bookstore application discussed in The Example JSP Pages, rewritten here to take advantage of several custom tags:

  • A catalog tag for rendering the book catalog

  • A shipDate tag for rendering the ship date of an order

  • A template library for ensuring a common look and feel among all screens and composing screens out of content chunks

The tutorial-template tag library defines a set of tags for creating an application template. The template is a JSP page that has placeholders for the parts that need to change with each screen. Each of these placeholders is referred to as a parameter of the template. For example, a simple template might include a title parameter for the top of the generated screen and a body parameter to refer to a JSP page for the custom content of the screen. The template is created using a set of nested tags (definition, screen, and parameter) that are used to build a table of screen definitions for Duke’s Bookstore. An insert tag to insert parameters from the table into the screen.

Figure 8-1 shows the flow of a request through the following Duke’s Bookstore web components:

  • tut-install/javaeetutorial5/examples/web/bookstore3/web/template/template.jsp, which determines the structure of each screen. It uses the insert tag to compose a screen from subcomponents.

  • tut-install/javaeetutorial5/examples/web/bookstore3/web/template/screendefinitions.jspf, which defines the subcomponents used by each screen. All screens have the same banner but different title and body content (specified by the JSP Pages column in Figure 5-1).

  • tut-install/javaeetutorial5/examples/web/bookstore3/src/java/com/sun/bookstore3/dispatcher/, a servlet, which processes requests and forwards to template.jsp.

Figure 8-1 Request Flow through Duke’s Bookstore Components

Diagram shows how a request is routed through a servlet and two JSP pages to the Book DB, then a response is routed through a JSP page to the client.

The source code for the Duke’s Bookstore application is located in the tut-install/javaeetutorial5/examples/web/bookstore3/ directory created when you unzip the tutorial bundle (see Chapter 2, Using the Tutorial Examples).

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

  1. Perform all the operations described in Accessing Databases from Web Applications.

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

  3. In the Open Project dialog, navigate to:

  4. Select the bookstore3 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 the bookstore3 project, and select Undeploy and Deploy.

  8. To run the application, open the bookstore URL http://localhost:8080/bookstore3/bookstore.

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

  1. In a terminal window, go to tut-install/javaeetutorial5/examples/web/bookstore3/.

  2. Type ant. This command will spawn any necessary compilations, copy files to the tut-install/javaeetutorial5/examples/web/bookstore3/build/ directory, and create a WAR file and copy it to the tut-install/javaeetutorial5/examples/web/bookstore3/dist/ directory.

  3. Start the Application Server.

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

  5. To deploy the example, type ant 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 URL http://localhost:8080/bookstore3/bookstore.

To learn how to configure the example, refer to the web.xml file, which includes the following configurations:

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

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

  • A listener element that identifies the ContextListener class used to create and remove the database access.

  • A servlet element that identifies the Dispatcher instance.

  • A set of servlet-mapping elements that map Dispatcher to URL patterns for each of the JSP pages in the application.

  • Nested inside a jsp-config element is a jsp-property-group element, which sets the properties for the group of pages included in this version of Duke’s Bookstore. See Setting Properties for Groups of JSP Pages for more information.

To run the example, open the bookstore URL http://localhost:8080/bookstore3/bookstore.

See Troubleshooting Duke's Bookstore Database Problems for help with diagnosing common problems.