J2EE Tutorial

J2EE Tutorial

Documents
517 pages
Lire
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres

Description

TMTHE
J2EE Tutorial TMTHE
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 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 TYPO-
GRAPHICAL ERRORS. CHANGES ARE PERIODICALLY ADDED TO THE INFORMA-
TION HEREIN; THESE 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
corpsales@pearsontechgroup.com
Visit ...

Sujets

Informations

Publié par
Nombre de visites sur la page 192
Langue English
Signaler un problème
TMTHE J2EE Tutorial TMTHE 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 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 TYPO- GRAPHICAL ERRORS. CHANGES ARE PERIODICALLY ADDED TO THE INFORMA- TION HEREIN; THESE 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 corpsales@pearsontechgroup.com 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 Contents 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 v 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