Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu J2EE TUTORIAL ppt
Nội dung xem thử
Mô tả chi tiết
STEPHANIE BODOFF
J2EE TUTORIAL
Ebook.moet.gov.vn, 2008
J2EE Tutorial THE TM
J2EE Tutorial
Stephanie Bodoff
Dale Green
Kim Haase
Eric Jendrock
Monica Pawlan
Beth Stearns
Boston • San Francisco • New York • Toronto • Montreal
London • Munich • Paris • Madrid
Capetown • Sydney • Tokyo • Singapore • Mexico City
THE TM
Copyright © 2002 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, CA 94303 USA.
All rights reserved.
Duke logo™ designed by Joe Palrang.
Sun, Sun Microsystems, Sun logo, Java, JDBC, 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 Company, Ltd. .
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 ANY TECHNOLOGY, PRODUCT, OR PROGRAM DESCRIBED
IN THIS PUBLICATION AT ANY TIME.
Pearson Education Corporate Sales Division
One Lake Street
Upper Saddle River, NJ 07458
(800) 382-3419
Visit Addison-Wesley on the Web: www.aw.com/cseng/
Library of Congress Control Number: 2002102527
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system,
or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording,
or otherwise, without the prior consent of the publisher. Printed in the United States of America.
Published simultaneously in Canada.
ISBN 0-201-79168-4
Text printed on recycled paper
1 2 3 4 5 6 7 8 9 10—MA—0605040302
First printing, March 2002
v
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi
Chapter 1: Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Distributed Multitiered Applications 2
J2EE Components 3
J2EE Clients 4
Web Components 6
Business Components 6
Enterprise Information System Tier 8
J2EE Containers 8
Container Services 8
Container Types 9
Packaging 10
Development Roles 11
J2EE Product Provider 12
Tool Provider 12
Application Component Provider 12
Application Assembler 13
Application Deployer and Administrator 14
Reference Implementation Software 14
Database Access 15
J2EE APIs 15
Simplified Systems Integration 18
Tools 19
Contents
vi CONTENTS
Chapter 2: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21
Setting Up 22
Getting the Example Code 22
Getting the Build Tool (ant) 22
Checking the Environment Variables 23
Starting the J2EE Server 23
Starting the deploytool 23
Creating the J2EE Application 24
Creating the Enterprise Bean 24
Coding the Enterprise Bean 24
Compiling the Source Files 26
Packaging the Enterprise Bean 26
Creating the J2EE Application Client 28
Coding the J2EE Application Client 28
Compiling the Application Client 31
Packaging the J2EE Application Client 31
Specifying the Application Client’s Enterprise Bean Reference 32
Creating the Web Client 32
Coding the Web Client 32
Compiling the Web Client 34
Packaging the Web Client 34
Specifying the Web Client’s Enterprise Bean Reference 35
Specifying the JNDI Names 35
Deploying the J2EE Application 37
Running the J2EE Application Client 37
Running the Web Client 38
Modifying the J2EE Application 39
Modifying a Class File 39
Adding a File 39
Modifying the Web Client 39
Modifying a Deployment Setting 40
Common Problems and Their Solutions 40
Cannot Start the J2EE Server 40
Compilation Errors 41
Deployment Errors 42
J2EE Application Client Runtime Errors 43
Web Client Runtime Errors 44
Detecting Problems With the Verifier Tool 45
Comparing Your EAR Files with Ours 45
When All Else Fails 45
CONTENTS vii
Chapter 3: Enterprise Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
What Is an Enterprise Bean? 48
Benefits of Enterprise Beans 48
When to Use Enterprise Beans 49
Types of Enterprise Beans 49
What Is a Session Bean? 49
State Management Modes 50
When to Use Session Beans 51
What Is an Entity Bean? 51
What Makes Entity Beans Different from Session Beans? 52
Container-Managed Persistence 53
When to Use Entity Beans 56
What Is a Message-Driven Bean? 56
What Makes Message-Driven Beans Different from Session
and Entity Beans? 57
When to Use Message-Driven Beans 57
Defining Client Access with Interfaces 58
Remote Access 58
Local Access 59
Local Interfaces and Container-Managed Relationships 59
Deciding on Remote or Local Access 60
Performance and Access 61
Method Parameters and Access 61
The Contents of an Enterprise Bean 62
Naming Conventions for Enterprise Beans 62
The Life Cycles of Enterprise Beans 63
The Life Cycle of a Stateful Session Bean 63
The Life Cycle of a Stateless Session Bean 64
The Life Cycle of an Entity Bean 65
The Life Cycle of a Message-Driven Bean 67
Chapter 4: A Session Bean Example . . . . . . . . . . . . . . . . . . . . . 69
The CartEJB Example 70
Session Bean Class 70
Home Interface 74
Remote Interface 76
Helper Classes 76
Running the CartEJB Example 76
Other Enterprise Bean Features 78
Accessing Environment Entries 78
Comparing Enterprise Beans 80
Passing an Enterprise Bean’s Object Reference 80
viii CONTENTS
Chapter 5: Bean-Managed Persistence Examples. . . . . . . . . .83
The SavingsAccountEJB Example 84
Entity Bean Class 84
Home Interface 94
Remote Interface 96
Running the SavingsAccountEJB Example 97
deploytool Tips for Entity Beans with
Bean-Managed Persistence 99
Mapping Table Relationships for Bean-Managed Persistence 99
One-to-One Relationships 99
One-to-Many Relationships 103
Many-to-Many Relationships 110
Primary Keys for Bean-Managed Persistence 113
The Primary Key Class 113
Primary Keys in the Entity Bean Class 115
Getting the Primary Key 116
Handling Exceptions 116
Chapter 6: Container-Managed Persistence Examples . . . .119
Overview of the RosterApp Application 120
The PlayerEJB Code 121
Entity Bean Class 122
Local Home Interface 126
Local Interface 127
A Guided Tour of the RosterApp Settings 128
RosterApp 128
RosterClient 129
RosterJAR 130
TeamJAR 131
Method Invocations in RosterApp 136
Creating a Player 137
Adding a Player to a Team 138
Removing a Player 139
Dropping a Player from a Team 140
Getting the Players of a Team 141
Getting a Copy of a Team’s Players 142
Finding the Players by Position 144
Getting the Sports of a Player 145
Running the RosterApp Example 147
Setting Up 147
Deploying the Application 147
Running the Client 148
CONTENTS ix
deploytool Tips for Entity Beans with
Container-Managed Persistence 148
Specifying the Bean’s Type 148
Selecting the Persistent Fields and Abstract Schema Name 149
Defining EJB QL Queries for Finder and Select Methods 149
Generating SQL and Specifying Table Creation 149
Specifying the Database JNDI Name, User Name,
and Password 150
Defining Relationships 150
Primary Keys for Container-Managed Persistence 151
The Primary Key Class 151
Primary Keys in the Entity Bean Class 152
Generating Primary Key Values 153
Chapter 7: A Message-Driven Bean Example . . . . . . . . . . . . 155
Example Application Overview 156
The J2EE Application Client 157
The Message-Driven Bean Class 157
The onMessage Method 158
The ejbCreate and ejbRemove Methods 159
Running the SimpleMessageEJB Example 159
Starting the J2EE Server 159
Creating the Queue 159
Deploying the Application 159
Running the Client 160
deploytool Tips for Message-Driven Beans 160
Specifying the Bean’s Type and Transaction Management 161
Setting the Message-Driven Bean’s Characteristics 161
deploytool Tips for JMS Clients 162
Setting the Resource References 162
Setting the Resource Environment References 163
Specifying the JNDI Names 163
Chapter 8: Enterprise JavaBeans Query Language . . . . . . . 165
Terminology 166
Simplified Syntax 167
x CONTENTS
Example Queries 167
Simple Finder Queries 167
Finder Queries That Navigate to Related Beans 169
Finder Queries with Other Conditional Expressions 170
Select Queries 172
Full Syntax 173
BNF Symbols 173
BNF Grammar of EJB QL 173
FROM Clause 176
Path Expressions 179
WHERE Clause 182
SELECT Clause 190
EJB QL Restrictions 191
Chapter 9: Web Clients and Components. . . . . . . . . . . . . . . .193
Web Client Life Cycle 194
Web Application Archives 196
Creating a WAR File 197
Adding a WAR File to an EAR File 197
Adding a Web Component to a WAR File 198
Configuring Web Clients 199
Application-Level Configuration 199
WAR-Level Configuration 200
Component-Level Configuration 202
Deploying Web Clients 203
Running Web Clients 203
Updating Web Clients 204
Internationalizing Web Clients 206
Chapter 10: Java Servlet Technology . . . . . . . . . . . . . . . . . . . .209
What Is a Servlet? 210
The Example Servlets 211
Troubleshooting 215
Servlet Life Cycle 216
Handling Servlet Life-Cycle Events 216
Handling Errors 218
Sharing Information 218
Using Scope Objects 219
Controlling Concurrent Access to Shared Resources 220
Accessing Databases 221
Initializing a Servlet 222
CONTENTS xi
Writing Service Methods 222
Getting Information from Requests 223
Constructing Responses 225
Filtering Requests and Responses 227
Programming Filters 229
Programming Customized Requests and Responses 230
Specifying Filter Mappings 232
Invoking Other Web Resources 234
Including Other Resources in the Response 234
Transferring Control to Another Web Component 236
Accessing the Web Context 237
Maintaining Client State 238
Accessing a Session 238
Associating Attributes with a Session 238
Session Management 239
Session Tracking 240
Finalizing a Servlet 241
Tracking Service Requests 242
Notifying Methods to Shut Down 242
Creating Polite Long-Running Methods 243
Chapter 11: JavaServer Pages Technology . . . . . . . . . . . . . . . 245
What Is a JSP Page? 246
The Example JSP Pages 249
The Life Cycle of a JSP Page 253
Translation and Compilation 253
Execution 254
Initializing and Finalizing a JSP Page 256
Creating Static Content 257
Creating Dynamic Content 257
Using Objects within JSP Pages 257
JSP Scripting Elements 260
Including Content in a JSP Page 263
Transferring Control to Another Web Component 265
Param Element 265
Including an Applet 265
Extending the JSP Language 267
Chapter 12: JavaBeans Components in JSP Pages. . . . . . . . . 269
JavaBeans Component Design Conventions 270
Why Use a JavaBeans Component? 271
xii CONTENTS
Creating and Using a JavaBeans Component 272
Setting JavaBeans Component Properties 273
Retrieving JavaBeans Component Properties 275
Chapter 13: Custom Tags in JSP Pages . . . . . . . . . . . . . . . . . . .279
What Is a Custom Tag? 280
The Example JSP Pages 281
Using Tags 285
Declaring Tag Libraries 285
Types of Tags 286
Defining Tags 289
Tag Handlers 289
Tag Library Descriptors 290
Simple Tags 293
Tags with Attributes 294
Tags With Bodies 296
Tags That Define Scripting Variables 298
Cooperating Tags 302
Examples 304
An Iteration Tag 304
A Template Tag Library 308
How Is a Tag Handler Invoked? 313
Chapter 14: Transactions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .315
What Is a Transaction? 316
Container-Managed Transactions 316
Transaction Attributes 317
Rolling Back a Container-Managed Transaction 321
Synchronizing a Session Bean’s Instance Variables 322
Methods Not Allowed in Container-Managed Transactions 323
Bean-Managed Transactions 323
JDBC Transactions 324
JTA Transactions 325
Returning without Committing 326
Methods Not Allowed in Bean-Managed Transactions 327
Summary of Transaction Options for Enterprise Beans 327
Transaction Timeouts 328
Isolation Levels 328
Updating Multiple Databases 329
Transactions in Web Components 331
CONTENTS xiii
Chapter 15: Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Overview 334
Security Roles 335
Declaring and Linking Role References 335
Mapping Roles to J2EE Users and Groups 337
Web-Tier Security 337
Protecting Web Resources 337
Controlling Access to Web Resources 338
Authenticating Users of Web Resources 338
Using Programmatic Security in the Web Tier 340
Unprotected Web Resources 340
EJB-Tier Security 340
Declaring Method Permissions 341
Using Programmatic Security in the EJB Tier 341
Unprotected EJB-Tier Resources 342
Application Client-Tier Security 342
Specifying the Application Client’s Callback Handler 343
EIS-Tier Security 343
Configuring Sign-On 344
Container-Managed Sign-On 344
Component-Managed Sign-On 344
Configuring Resource Adapter Security 345
Propagating Security Identity 346
Configuring a Component’s Propagated Security Identity 346
Configuring Client Authentication 347
J2EE Users, Realms, and Groups 348
Managing J2EE Users and Groups 349
Setting Up a Server Certificate 350
Chapter 16: Resource Connections . . . . . . . . . . . . . . . . . . . . . 353
JNDI Names and Resource References 354
deploytool Tips for Resource References 354
Database Connections for Enterprise Beans 357
Coded Connections 357
Connection Pooling 359
Mail Session Connections 359
Running the ConfirmerEJB Example 361
URL Connections 362
Running the HTMLReaderEJB Example 363
xiv CONTENTS
Chapter 17: J2EE Connector Architecture. . . . . . . . . . . . . . . . .365
About Resource Adapters 366
Resource Adapter Contracts 366
Administering Resource Adapters 368
The Black Box Resource Adapters 369
Transaction Levels 369
Properties 370
Configuring JDBC Drivers 371
Resource Adapter Tutorial 372
Setting Up 372
Deploying the Resource Adapter 372
Testing the Resource Adapter 373
Common Client Interface 375
Overview of the CCI 375
Programming with the CCI 376
Writing a CCI Client 385
CCI Tutorial 386
Chapter 18: The Duke’s Bank Application. . . . . . . . . . . . . . . . .391
Enterprise Beans 393
Session Beans 394
Entity Beans 397
Helper Classes 397
Database Tables 398
Protecting the Enterprise Beans 400
Application Client 400
The Classes and Their Relationships 401
BankAdmin Class 403
EventHandle Class 404
DataModel Class 405
Web Client 408
Design Strategies 409
Web Client Life Cycle 410
Protecting the Web Resources 414
Internationalization 414