Tải bản đầy đủ (.pdf) (35 trang)

The j2eetm tutorial - phần 1 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.1 MB, 35 trang )

The J2EE
TM
Tutorial
Stephanie Bodoff
Dale Green
Eric Jendrock
Monica Pawlan
Beth Stearns
Simpo PDF Merge and Split Unregistered Version -
Copyright © 2001 by Sun Microsystems, Inc.
901 San Antonio Road, Palo Alto, California 94303 U.S.A.
All rights reserved.
RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the United States Government is
subject to the restrictions set forth in DFARS 252.227-7013(c)(1)(iii) and FAR 52.227-19.
The release described in this book may be protected by one or more U.S. patents, foreign patents, or
pending applications.
Sun, Sun Microsystems, Sun Microsystems Computer Corporation, the Sun logo, the Sun Microsystems
Computer Corporation logo, Java, JavaSoft, Java Software, JavaScript, JDBC, JDBC Compliant, JavaOS,
JavaBeans, Enterprise JavaBeans, JavaServer Pages, J2EE, J2SE, JavaMail, Java Naming and Directory
Interface, EJB, and JSP are trademarks or registered trademarks of Sun Microsystems, Inc. UNIX
®
is a
registered trademark in the United States and other countries, exclusively licensed through X/Open Com-
pany, Ltd. All other product names mentioned herein are the trademarks of their respective owners.
THIS PUBLICATION IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
THIS PUBLICATION COULD INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL
ERRORS. CHANGES ARE PERIODICALLY ADDED TO THE INFORMATION HEREIN; THESE
CHANGES WILL BE INCORPORATED IN NEW EDITIONS OF THE PUBLICATION. SUN
MICROSYSTEMS, INC. MAY MAKE IMPROVEMENTS AND/OR CHANGES IN THE PROD-


UCT(S) AND/OR THE PROGRAM(S) DESCRIBED IN THIS PUBLICATION AT ANY TIME.
Simpo PDF Merge and Split Unregistered Version -
iii
Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Who Should Use This Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
About the Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xviii
Prerequisites for the Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xviii
Where to Find the Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xviii
How to Build and Run the Examples. . . . . . . . . . . . . . . . . . . . . . . . .xviii
Related Information. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx
How to Print This Tutorial. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Typographical Conventions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii
Chapter 1: Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Distributed Multitiered Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
J2EE Application Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Client Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Application Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Web Browsers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
JavaBeans™ Component Architecture . . . . . . . . . . . . . . . . . . . . . 27
J2EE Server Communications. . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Thin Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Web Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Business Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Enterprise Information System Tier. . . . . . . . . . . . . . . . . . . . . . . . . . . 30
J2EE Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Containers and Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Container Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

Packaging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Simpo PDF Merge and Split Unregistered Version -
iv
Development Roles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34
J2EE Product Provider. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34
Tool Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34
Application Component Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
Enterprise Bean Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
Web Component Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
J2EE Application Client Creation. . . . . . . . . . . . . . . . . . . . . . . . . .35
Application Assembler. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
Application Deployer and Administrator. . . . . . . . . . . . . . . . . . . . . . . .36
Reference Implementation Software. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36
Web Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
Database Access. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
J2EE APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
Enterprise JavaBeans Technology 2.0 . . . . . . . . . . . . . . . . . . . . . .37
JDBC™ 2.0 API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38
Java Servlet Technology 2.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38
JavaServer Pages (JSP) Technology 1.2. . . . . . . . . . . . . . . . . . . . .38
Java Message Service (JMS) 1.0 . . . . . . . . . . . . . . . . . . . . . . . . . .39
Java Transaction API (JTA) 1.0 . . . . . . . . . . . . . . . . . . . . . . . . . . .39
JavaMail™ Technology 1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39
JavaBeans Activation Framework 1.0 . . . . . . . . . . . . . . . . . . . . . .39
Java API for XML (JAXP) 1.1. . . . . . . . . . . . . . . . . . . . . . . . . . . .39
J2EE Connector API 1.0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40
Java Authentication and Authorization Service (JAAS) 1.0 . . . . .40
Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40
Application Deployment Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . .40
Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42

Chapter 2: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43
Setting Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44
Getting the Example Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44
Building the Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44
Checking the Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . .45
Starting the J2EE™ Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45
Starting the deploytool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45
Creating the J2EE™ Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46
Creating the Enterprise Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46
Coding the Enterprise Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46
Coding the Remote Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46
Coding the Home Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47
Coding the Enterprise Bean Class . . . . . . . . . . . . . . . . . . . . . . . . .47
Compiling the Source Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48
Packaging the Enterprise Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48
Simpo PDF Merge and Split Unregistered Version -
v
Creating the J2EE™ Application Client . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Coding the J2EE Application Client . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Locating the Home Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Creating an Enterprise Bean Instance . . . . . . . . . . . . . . . . . . . . . . 51
Invoking a Business Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
ConverterClient Source Code . . . . . . . . . . . . . . . . . . . . . . . . 51
Compiling the Application Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Packaging the J2EE Application Client . . . . . . . . . . . . . . . . . . . . . . . . 52
Specifying the Application Client’s Enterprise Bean Reference . . . . . 53
Creating the Web Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Coding the Web Client. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Compiling the Web Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Packaging the Web Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

Specifying the Web Client’s Enterprise Bean Reference. . . . . . . . . . . 56
Specifying the JNDI Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Deploying the J2EE™ Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Running the J2EE™ Application Client . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Running the Web Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Modifying the J2EE™ Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Modifying a Class File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Adding a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Modifying the Web Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Modifying a Deployment Setting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Common Problems and Their Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Cannot Start the J2EE Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Naming and Directory Service Port Conflict . . . . . . . . . . . . . . . . 61
Web Service Port Conflict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Compilation Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Ant Cannot Locate the Build File . . . . . . . . . . . . . . . . . . . . . . . . . 61
The Compiler Cannot Resolve Symbols. . . . . . . . . . . . . . . . . . . . 62
J2EE Application Client Runtime Errors. . . . . . . . . . . . . . . . . . . . . . . 62
The Client Cannot Find ConverterApp.ear. . . . . . . . . . . . . . . . . . 62
The Client Cannot Find the ConverterClient Component. . . . . . . 62
The Login Failed. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
The J2EE Application Has Not Been Deployed . . . . . . . . . . . . . . 63
The JNDI Name is Incorrect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Web Client Runtime Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
The Web Context in the URL is Incorrect . . . . . . . . . . . . . . . . . . 63
The J2EE Application Has Not Been Deployed . . . . . . . . . . . . . . 63
The JNDI Name is Incorrect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Deployment Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Simpo PDF Merge and Split Unregistered Version -
vi

Chapter 3: Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65
A Session Bean Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66
Session Bean Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66
The SessionBean Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . .68
The ejbCreate Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68
Business Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69
Home Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70
Remote Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71
Helper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72
State Management Modes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72
Stateful Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72
Stateless Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73
Choosing Between Stateful and Stateless Session Beans . . . . . . . . . . .73
The Life Cycle of a Session Bean. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .74
The Stateful Session Bean Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . .74
The Stateless Session Bean Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . .75
Other Enterprise Bean Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76
Accessing Environment Entries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76
Comparing Enterprise Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77
Passing an Enterprise Bean’s Object Reference . . . . . . . . . . . . . . . . . .78
Chapter 4: Entity Beans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81
Characteristics of Entity Beans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82
Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82
Bean-Managed Persistence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82
Container-Managed Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . .82
Shared Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83
Primary Key. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83
Simpo PDF Merge and Split Unregistered Version -
vii
A Bean-Managed Persistence Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

Entity Bean Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
The EntityBean Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
The ejbCreate Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
The ejbPostCreate Method . . . . . . . . . . . . . . . . . . . . . . . . . . 86
The ejbRemove Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
The ejbLoad and ejbStore Methods . . . . . . . . . . . . . . . . . . . 87
The Finder Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
The Business Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Database Calls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Home Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Remote Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Tips for Running the AccountEJB Example . . . . . . . . . . . . . . 94
Setting Up the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Running the New Enterprise Bean Wizard. . . . . . . . . . . . . . . . . . 95
Deploying the J2EE Application. . . . . . . . . . . . . . . . . . . . . . . . . . 95
Running the J2EE Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Mapping Table Relationships For Bean-Managed Persistence. . . . . . . . . . 96
One-to-One Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Tips for Running the StorageBinEJB Example . . . . . . . . . . . . 99
One-to-Many Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
A Helper Class for the Child Table. . . . . . . . . . . . . . . . . . . . . . . 101
Tips for Running the OrderEJB Example: . . . . . . . . . . . . . . . 104
An Entity Bean for the Child Table . . . . . . . . . . . . . . . . . . . . . . 104
Tips for Running the SalesRepEJB Example: . . . . . . . . . . . . 107
Many-to-Many Relationships. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Tips for running the EnrollerEJB example: . . . . . . . . . . . . . . . 110
About Container-Managed Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
A Container-Managed Persistence Example. . . . . . . . . . . . . . . . . . . . . . . 111
Primary Key Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Creating a Primary Key Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

Class Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Bean-Managed Persistence and the Primary Key Class. . . . . . . . . . . 113
Container-Managed Persistence and the Primary Key Class. . . . . . . 114
Getting the Primary Key. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
The Life Cycle of an Entity Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Chapter 5: Web Components . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Web Component Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Simpo PDF Merge and Split Unregistered Version -
viii
Packaging Web Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122
Creating a WAR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122
Adding a WAR to a J2EE Application . . . . . . . . . . . . . . . . . . . . . . . .123
Adding a Web Component to a WAR . . . . . . . . . . . . . . . . . . . . . . . . .123
Configuring Web Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124
Application-Level Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . .125
Context Root. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125
WAR-Level Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125
Context Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125
References to Environment Entries, Enterprise Beans, Resource En-
vironment Entries, or Resources. . . . . . . . . . . . . . . . . . . . . . . . . .126
Event Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126
Error Mapping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126
Filter Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127
Component-Level Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . .128
Initialization Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128
Specifying an Alias Path. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128
Deploying Web Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129
Executing Web Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129
Updating Web Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129

Chapter 6: Java Servlet Technology . . . . . . . . . . . . . . . . . . . . . .133
What is a Servlet? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134
The Example Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135
Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138
Servlet Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139
Monitoring Servlet Life Cycle Events . . . . . . . . . . . . . . . . . . . . . . . . .140
Defining The Listener Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . .140
Specifying Event Listener Classes . . . . . . . . . . . . . . . . . . . . . . . .141
Handling Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142
Sharing Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142
Scope Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142
Controlling Concurrent Access to Shared Resources . . . . . . . . . . . . .143
Initializing a Servlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145
Writing Service Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145
Getting Information From Requests . . . . . . . . . . . . . . . . . . . . . . . . . .146
Constructing Responses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148
Filtering Requests and Responses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151
Defining the Filter Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152
Specifying Filter Mappings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .155
Simpo PDF Merge and Split Unregistered Version -
ix
Invoking Other Web Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Including the Content of Another Resource in the Response . . . . . . 157
Transferring a Control to Another Web Component . . . . . . . . . . . . . 158
Accessing the Web Context. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Maintaining Client State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Accessing a Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Associating Attributes with a Session . . . . . . . . . . . . . . . . . . . . . . . . 161
Notifying Objects That Are Added To a Session . . . . . . . . . . . . 161
Session Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

Session Tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Finalizing a Servlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Tracking Service Requests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Providing a Clean Shutdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Creating Polite Long-Running Methods . . . . . . . . . . . . . . . . . . . . . . 166
Chapter 7: JavaServer Pages™ Technology. . . . . . . . . . . . . . . 167
What is a JSP Page?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
The Example JSP Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
The Life Cycle of a JSP Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Translation and Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Buffering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Handling Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Initializing and Finalizing a JSP Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Creating Static Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Creating Dynamic Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Using Objects Within JSP Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Implicit Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Application-Specific Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Shared Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
JSP Scripting Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Declarations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Scriptlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Including Content in a JSP Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Transferring Control to Another Web Component . . . . . . . . . . . . . . . . . . 186
Param Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Including an Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Extending the JSP Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Simpo PDF Merge and Split Unregistered Version -

x
Chapter 8: JavaBeans™ Components in JSP™ Pages . . . . . . .191
JavaBeans Component Design Conventions. . . . . . . . . . . . . . . . . . . . . . . .192
Why Use a JavaBeans Component? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .193
Creating and Using a JavaBeans Component . . . . . . . . . . . . . . . . . . . . . . .194
Setting JavaBeans Component Properties. . . . . . . . . . . . . . . . . . . . . . . . . .194
Retrieving JavaBeans Component Properties. . . . . . . . . . . . . . . . . . . . . . .197
Chapter 9: Custom Tags in JSP™ Pages . . . . . . . . . . . . . . . . . . .199
What is a Custom Tag? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200
The Example Tags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200
Using Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .204
Declaring Tag Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .204
Types of Tags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205
Simple Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205
Tags With Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205
Tags With Bodies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .207
Choosing Between Passing Information as Attributes or Body . .207
Tags That Define Scripting Variables . . . . . . . . . . . . . . . . . . . . .207
Cooperating Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .208
Defining Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .208
Tag Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .209
Tag Library Descriptors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .210
Listener Element. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211
Tag Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211
Simple Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .212
Tag Handlers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .212
Body-content Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .213
Tags With Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .213
Defining Attributes in a Tag Handler . . . . . . . . . . . . . . . . . . . . . .213
Attribute Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214

Attribute Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215
Tags With Bodies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .216
Tag Handlers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .216
Body-content Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .218
Tags That Define Scripting Variables . . . . . . . . . . . . . . . . . . . . . . . . .218
Tag Handlers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .218
Providing Information About the Scripting Variable . . . . . . . . . .219
Cooperating Tags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .222
Simpo PDF Merge and Split Unregistered Version -
xi
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
An Iteration Tag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
JSP Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Tag Handler. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Tag Extra Info Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
A Template Tag Library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
JSP Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Tag Handlers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
How Is a Tag Handler Invoked? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Chapter 10: Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
What is a Transaction? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Container-Managed Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Transaction Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Transaction Attribute Values . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Required . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
RequiresNew. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Mandatory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
NotSupported . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Supports. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Never. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239

Summary of Transaction Attributes . . . . . . . . . . . . . . . . . . . . . . 239
Setting Transaction Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Rolling Back a Container-Managed Transaction. . . . . . . . . . . . . . . . 241
Synchronizing a Session Bean’s Instance Variables . . . . . . . . . . . . . 242
Methods Not Allowed in Container-Managed Transactions . . . . . . . 243
Bean-Managed Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
JDBC Transactions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
JTA Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Returning Without Committing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Methods Not Allowed in Bean-Managed Transactions . . . . . . . . . . . 247
Summary of Transaction Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Transaction Timeouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Isolation Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Updating Multiple Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Simpo PDF Merge and Split Unregistered Version -
xii
Chapter 11: Security. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .253
Authentication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .254
J2EE Users, Realms, and Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . .255
Managing J2EE Users and Groups. . . . . . . . . . . . . . . . . . . . . . . .256
Authentication Mechanisms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .257
Web-Tier Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .257
Configuring A Web Component’s Authentication Mechanism . .258
Using Hybrid Authentication to Enhance Confidentiality . . . . . .258
Controlling Access to J2EE Resources . . . . . . . . . . . . . . . . . . . . . . . .259
Controlling Access to Web Resources . . . . . . . . . . . . . . . . . . . . .259
Controlling Access to Enterprise Beans . . . . . . . . . . . . . . . . . . . .259
Unprotected Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260
Client Authentication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260
Auto-Registration of Users. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261

Web Client Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261
Data Protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261
Developing a Custom Web Tier User Authentication Interface. .261
Application Client Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . .261
Setting Component Security Identities . . . . . . . . . . . . . . . . . . . . . . . .262
Capturing a Security Context (Servlet). . . . . . . . . . . . . . . . . . . . .262
Container Authentication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .262
Setting Up a Server Certificate. . . . . . . . . . . . . . . . . . . . . . . . . . .262
Configuring EJB Target Security Requirements . . . . . . . . . . . . . . . . .263
Configuring Resource Signon (Connectors) . . . . . . . . . . . . . . . . . . . .264
Authorization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .264
Declaring Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .265
Declaring Method Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .265
Declaring Role References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .266
Mapping Roles to J2EE Users and Groups . . . . . . . . . . . . . . . . . . . . .266
Linking Role References to Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . .267
Configuring J2SE Security Policy Files . . . . . . . . . . . . . . . . . . . . . . .267
Determining the Caller Identity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .267
Making Portable Access Decisions Programmatically from Components
267
Protecting Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .268
Application Scenarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .268
J2EE Application Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .268
Web Browser Client. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .268
Simpo PDF Merge and Split Unregistered Version -
xiii
Chapter 12: Resource Connections. . . . . . . . . . . . . . . . . . . . . . . 269
JNDI Names and Resource References. . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Specifying a Resource Reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . 270

Mapping a Resource Reference to a JNDI Name. . . . . . . . . . . . . . . . 271
Database Connections for Enterprise Beans . . . . . . . . . . . . . . . . . . . . . . . 271
Coded Connections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
How to Connect. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
When To Connect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Specifying Database Users and Passwords. . . . . . . . . . . . . . . . . 273
Container-Managed Connections. . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Connection Pooling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Mail Session Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Tips for Running the ConfirmerEJB Example. . . . . . . . . . . . . . . . . . 275
URL Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Tips for Running the HTMLReaderEJB Example. . . . . . . . . . . . . . . 277
Chapter 13: J2EE™Connector Technology . . . . . . . . . . . . . . . . . 281
About Resource Adapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Resource Adapter Contracts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Administering Resource Adapters . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
The Black Box Resource Adapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Transaction Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Configuring JDBC™ Drivers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
The Non-XA Black Box Adapters . . . . . . . . . . . . . . . . . . . . . . . 287
The XA Black Box Adapters . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Resource Adapter Tutorial. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Setting Up. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Deploying the Resource Adapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Testing the Resource Adapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Simpo PDF Merge and Split Unregistered Version -
xiv
Common Client Interface (CCI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .291
Overview of the CCI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .291

Programming with the CCI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .293
Database Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . .295
Mapping to Stored Procedure Parameters . . . . . . . . . . . . . . . . . .296
Reading Database Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .298
Inserting Database Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . .300
Writing a CCI Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .302
CCI Tutorial. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .302
Deploy the Resource Adapter. . . . . . . . . . . . . . . . . . . . . . . . . . . .302
Set Up the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303
Create the J2EE Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . .304
Test the Resource Adapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .306
Chapter 14: HTTP Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .307
HTTP Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .307
HTTP Responses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .308
Chapter 15: J2EE

SDK Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . .309
J2EE Administration Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .310
Cleanup Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .311
Cloudscape Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .311
Starting and Stopping Cloudscape. . . . . . . . . . . . . . . . . . . . . . . . . . . .311
Cloudscape Server Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . .312
Cloudscape ij Tool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .312
Deployment Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .313
J2EE Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .314
Key Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .314
Simpo PDF Merge and Split Unregistered Version -
xv
Packager. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
EJB JAR File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315

Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Web Application WAR File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Example: Creating a Simple WAR File . . . . . . . . . . . . . . . . . . . 316
Example: Specifying Individual Content Files . . . . . . . . . . . . . . 316
Example: Specifying Servlets and JSP Files. . . . . . . . . . . . . . . . 316
Application Client JAR File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
J2EE Application EAR File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Example: Creating an EAR File . . . . . . . . . . . . . . . . . . . . . . . . . 318
Example: Specifying the Runtime Deployment Descriptor . . . . 318
Resource Adapter RAR File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Realm Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Runclient Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Verifier. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Command-Line Verifier. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Stand-Alone GUI Verifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Simpo PDF Merge and Split Unregistered Version -
xvi
Simpo PDF Merge and Split Unregistered Version -
xvii
Preface
THE Java Tutorial has been an indispensable resource for many programmers
learning the Java programming language. This tutorial hopes to serve the same
role for developers encountering the Java™ 2 Platform, Enterprise Edition

(J2EE™) for the first time. It follows an example-oriented focus similar to the
Java Tutorial.
Who Should Use This Tutorial xvii
About the Examples xviii
Related Information xx
How to Print This Tutorial xxi
Typographical Conventions xxi
Acknowledgments xxii
Who Should Use This Tutorial
This tutorial is intended for programmers interested in developing and deploying
J2EE applications. It covers the main component technologies comprising the
J2EE platform and describes how to develop J2EE components and deploy them
on the J2EE SDK.
This tutorial is not intended for J2EE server or tools vendors. It does not explain
how to implement the J2EE architecture, nor does it explain the internals of the
J2EE SDK. The J2EE specifications describe the J2EE architecture and can be
downloaded from:
/>Simpo PDF Merge and Split Unregistered Version -
xviii
About the Examples
This tutorial includes many complete, working examples.
Prerequisites for the Examples
To understand the examples you will need a good knowledge of the Java pro-
gramming language, SQL, and relational database concepts. The following top-
ics in the Java Tutorial are particularly relevant:
Where to Find the Examples
If you are viewing this online, and you want to build and run the examples, you
need to download the tutorial bundle from:
/>Once you have installed the bundle, the example code is in the examples/src
directory, with subdirectories ejb for enterprise bean technology examples, web

for web technology examples, and connector for connector technology exam-
ples.
How to Build and Run the Examples
This tutorial documents the J2EE SDK version 1.3. To build, deploy, and run the
examples you need a copy of the J2EE SDK 1.3 and the J2SE™ SDK 1.3, also
known as a JDK. You can download the J2EE SDK from:
/>Topic Java Tutorial
JDBC™
/>Threads
/>JavaBeans™
/>Security
/>Simpo PDF Merge and Split Unregistered Version -
xix
and the J2SE 1.3 from:
/>The examples are distributed with a configuration file for ant version 1.2. A por-
table make tool,
ant is hosted by the Jakarta project at the Apache Software
Foundation. You can download
ant from:
/>To build the examples:
1. Download and install the J2SE SDK 1.3, J2EE SDK 1.3, and
ant.
2. The installation instructions for the J2SE SDK, J2EE SDK, and
ant
explain how to set the required environment variables. Verify that the envi-
ronment variables have been set to the values noted in the following table.
3. Go to the
examples/src directory.
4. Open the file
build.xml in a text editor. Edit the line:

<property name="j2ee-home" value="C:\j2sdkee1.3" />
and set the value of the j2ee-home property to be the J2EE SDK installa-
tion directory.
5. Execute
ant target. For example, to build all the examples, execute ant
all
or tobuild the web layer examples, execute ant web. The build process
deposits the output into the directory
examples/build.
Environment Variable Value
JAVA_HOME
The location of the J2SE SDK installation.
J2EE_HOME
The location of the J2EE SDK installation.
ANT_HOME
The location of the ant installation.
PATH
Should include the bin directory of the J2EE SDK and ant
installation.
Simpo PDF Merge and Split Unregistered Version -
xx
Related Information
This tutorial provides a concise overview of how to use the central component
technologies in the J2EE platform. For more information about these technolo-
gies, see:
The J2EE platform includes a wide variety of APIs that this tutorial only briefly
touches on. Some of these technologies have their own tutorials:
For complete information on these topics see:
Component Technology Web Site
Enterprise JavaBeans™ (EJB™)

/>Java Servlet
/>JavaServer Pages™ (JSP™)
/>API Tutorial
Java Message Service (JMS)
/>Java Naming and Directory
Interface™ (JNDI)
/>Extensible Markup Language
(XML)
/>API Web Site
XML
/>J2EE Connector
/>Simpo PDF Merge and Split Unregistered Version -
xxi
Once you have become familiar with the J2EE technologies described in this
tutorial, you may be interested in guidelines for architecting J2EE applications.
The J2EE Blueprints is a book and sample application that illustrate best prac-
tices for developing and deploying J2EE applications. You can obtain the J2EE
Blueprints from:
/>How to Print This Tutorial
To print this tutorial, follow these steps:
• Ensure that Adobe Acrobat Reader is installed on your system.
• Using your browser, access the PDF version of this book.
• Click the printer icon in Adobe Acrobat Reader.
Typographical Conventions
The following table lists the typographical conventions used in this tutorial.
JavaMail™
/>JMS
/>JNDI
/>JDBC™
/>Font Style Uses

italic
Emphasis, titles, first occurrence of terms
monospace URLs, code examples, file names, command
names, programming language keywords
italic monospace Programming variables, variable file names
API Web Site
Simpo PDF Merge and Split Unregistered Version -
xxii
Acknowledgments
The J2EE tutorial team would like to thank the J2EE SDK team for their techni-
cal advice and enthusiasm.
We would also like to thank our manager Jim Inscore for his support and steady-
ing influence.
The chapters on web components use an example and some material that first
appeared in the servlet trail of the Java Tutorial. The chapter on custom tags
describes a template tag library that first appeared in the J2EE Blueprints.
Simpo PDF Merge and Split Unregistered Version -
23
Overview
by Monica Pawlan
TODAY, more and more developers want to write distributed transactional
applications for the enterprise and leverage the speed, security, and reliability of
server-side technology. If you are already working in this area, you know that in
today’s fast-moving and demanding world of e-commerce and information tech-
nology, enterprise applications have to be designed, built, and produced for less
money, faster, and with fewer resources than ever before.
To reduce costs and fast-track enterprise application design and development,
the Java™ 2 Platform, Enterprise Edition (J2EE™) technology provides a com-
ponent-based approach to the design, development, assembly, and deployment of
enterprise applications. The J2EE platform gives you a multitiered distributed

application model, the ability to reuse components, a unified security model, and
flexible transaction control. Not only can you deliver innovative customer solu-
tions to market faster than ever, but your platform-independent J2EE compo-
nent-based solutions are not tied to the products and APIs of any one vendor.
This tutorial takes an examples-based approach to describing the features and
functionalities available in J2EE SDK version 1.3. Whether you are a new or an
experienced enterprise developer, you should find the examples and accompany-
ing text a valuable and accessible knowledge base for creating your own enter-
prise solutions.
If you are new to J2EE applications development, this chapter is a good place to
start. Here you will learn the J2EE architecture, become acquainted with impor-
tant terms and concepts, and find out how to approach J2EE applications pro-
gramming, assembly, and deployment.
Simpo PDF Merge and Split Unregistered Version -
24 OVERVIEW
Distributed Multitiered Applications 24
J2EE Application Components 25
Client Components 26
Thin Clients 28
Web Components 28
Business Components 29
Enterprise Information System Tier 30
J2EE Architecture 30
Containers and Services 30
Container Types 32
Packaging 33
Development Roles 34
J2EE Product Provider 34
Tool Provider 34
Application Component Provider 35

Application Assembler 35
Application Deployer and Administrator 36
Reference Implementation Software 36
Web Server 37
Database Access 37
J2EE APIs 37
Tools 40
Distributed Multitiered Applications
The J2EE platform uses a multitiered distributed application model. This means
application logic is divided into components according to function, and the vari-
ous application components that make up a J2EE application are installed on dif-
ferent machines depending on which tier in the multitiered J2EE environment
the application component belongs. Figure 1 shows two multitiered J2EE appli-
cations divided into the tiers described in the bullet list below. The J2EE applica-
tion parts shown in Figure 1 are presented in
J2EE Application
Components (page 25).
• Client tier components run on the client machine
• Web tier components run on the J2EE server
• Business tier components run on the J2EE server
• Enterprise information system (EIS) tier software runs on the EIS server
While a J2EE application can consist of the three or four tiers shown in Figure 1,
J2EE multitiered applications are generally considered to be three-tiered appli-
cations because they are distributed over three different locations: client
Simpo PDF Merge and Split Unregistered Version -
DISTRIBUTED MULTITIERED APPLICATIONS 25
machines, J2EE server machine, and the database or legacy machines at the
back-end. Three-tiered applications that run in this way extend the standard two-
tiered client and server model by placing a multithreaded application server
between the client application and back-end storage.

Figure 1 Multitiered Applications
J2EE Application Components
J2EE applications are made up of components. A J2EE component is a self-con-
tained functional software unit that is assembled into a J2EE application with its
related classes and files and communicates with other components. The J2EE
specification defines the following J2EE components:
• Application clients and applets are client components.
• Java Servlet and JavaServer Pages™ (JSP™) technology components are
web components.
• Enterprise JavaBeans™ (EJB™) components (enterprise beans) are busi-
ness components.
J2EE components are written in the Java programming language and compiled
in the same way as any Java programming language program. The difference
when you work with the J2EE platform, is J2EE components are assembled into
Application
Client
Enterprise
Beans
Dynamic
HTML pages
JSP pages
Enterprise
Beans
Database
Client
tier
Web
tier
Business
tier

EIS
tier
Client
Machine
J2EE
Server
Machine
Database
Server
Machine
Database
J2EE
Application 1
J2EE
Application 2
Simpo PDF Merge and Split Unregistered Version -

×