Transcription of THEJ2EE Tutorial - Computação UFCG
1 J2EE TutorialTHETMJ2EE TutorialStephanie BodoffDale GreenKim HaaseEric JendrockMonica PawlanBeth StearnsBoston San Francisco New York Toronto MontrealLondon Munich Paris MadridCapetown Sydney Tokyo Singapore Mexico CityTHETMC opyright 2002 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, CA 94303 rights logo designed by Joe , 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 registeredtrademarks of Sun Microsystems, Inc. UNIX is a registered trademark in the United States and othercountries, 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 IMPLIEDWARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, PUBLICATION COULD INCLUDE TECHNICAL INACCURACIES OR TYPO-GRAPHICAL ERRORS.
2 CHANGES ARE PERIODICALLY ADDED TO THE INFORMA-TION HEREIN; THESE CHANGES WILL BE INCORPORATED IN NEW EDITIONS OFTHE PUBLICATION. SUN MICROSYSTEMS, INC., MAY MAKE IMPROVEMENTSAND/OR CHANGES IN ANY TECHNOLOGY, PRODUCT, OR PROGRAM DESCRIBEDIN THIS PUBLICATION AT ANY Education Corporate Sales DivisionOne Lake StreetUpper Saddle River, NJ 07458(800) Addison-Wesley on the Web: 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 simultaneously in 0-201-79168-4 Text printed on recycled paper1 2 3 4 5 6 7 8 9 10 MA 0605040302 First printing, March 2002vForeword.
3 XviiPreface ..xxiChapter 1: Overview .. 1 Distributed Multitiered Applications 2J2EE Components3J2EE Clients4 Web Components6 Business Components6 Enterprise Information System Tier8J2EE Containers 8 Container Services8 Container Types9 Packaging 10 Development Roles 11J2EE Product Provider12 Tool Provider12 Application Component Provider12 Application Assembler13 Application Deployer and Administrator14 reference Implementation Software 14 Database Access15J2EE APIs15 Simplified Systems Integration18 Tools19 ContentsviCONTENTSC hapter 2: Getting Started ..21 Setting Up 22 Getting the Example Code22 Getting the Build Tool (ant)22 Checking the Environment Variables23 Starting the J2EE Server23 Starting the deploytool23 Creating the J2EE Application 24 Creating the Enterprise Bean 24 Coding the Enterprise Bean24 Compiling the Source Files26 Packaging the Enterprise Bean26 Creating the J2EE Application Client 28 Coding the J2EE Application Client28 Compiling the Application Client31 Packaging the J2EE Application Client31 Specifying the Application Client s Enterprise Bean Reference32 Creating the Web Client 32 Coding the Web Client32 Compiling the Web Client34 Packaging the Web Client34 Specifying the Web Client s Enterprise Bean Reference35 Specifying the JNDI Names 35 Deploying the J2EE Application 37 Running the J2EE Application Client 37 Running the Web Client 38 Modifying the J2EE
4 Application 39 Modifying a Class File39 Adding a File39 Modifying the Web Client39 Modifying a Deployment Setting40 Common Problems and Their Solutions 40 Cannot Start the J2EE Server40 Compilation Errors41 Deployment Errors42J2EE Application Client Runtime Errors43 Web Client Runtime Errors44 Detecting Problems With the Verifier Tool45 Comparing Your EAR Files with Ours45 When All Else Fails45 CONTENTSviiChapter 3: Enterprise Beans .. 47 What Is an Enterprise Bean? 48 Benefits of Enterprise Beans48 When to Use Enterprise Beans49 Types of Enterprise Beans49 What Is a Session Bean? 49 State Management Modes50 When to Use Session Beans51 What Is an Entity Bean? 51 What Makes Entity Beans Different from Session Beans?52 Container-Managed Persistence53 When to Use Entity Beans56 What Is a Message-Driven Bean?
5 56 What Makes Message-Driven Beans Different from Sessionand Entity Beans?57 When to Use Message-Driven Beans57 Defining Client Access with Interfaces 58 Remote Access58 Local Access59 Local Interfaces and Container-Managed Relationships59 Deciding on Remote or Local Access60 Performance and Access61 Method Parameters and Access61 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 Bean63 The Life Cycle of a Stateless Session Bean64 The Life Cycle of an Entity Bean65 The Life Cycle of a Message-Driven Bean67 Chapter 4: A Session Bean Example .. 69 The CartEJB Example 70 Session Bean Class70 Home Interface74 Remote Interface76 Helper Classes76 Running the CartEJB Example76 Other Enterprise Bean Features 78 Accessing Environment Entries78 Comparing Enterprise Beans80 Passing an Enterprise Bean s Object Reference80viiiCONTENTSC hapter 5: Bean-Managed Persistence Examples.
6 83 The SavingsAccountEJB Example 84 Entity Bean Class84 Home Interface94 Remote Interface96 Running the SavingsAccountEJB Example97deploytool Tips for Entity Beans withBean-Managed Persistence 99 Mapping Table Relationships for Bean-Managed Persistence 99 One-to-One Relationships99 One-to-Many Relationships103 Many-to-Many Relationships110 Primary Keys for Bean-Managed Persistence 113 The Primary Key Class113 Primary Keys in the Entity Bean Class115 Getting the Primary Key116 Handling Exceptions 116 Chapter 6: Container-Managed Persistence Examples ..119 Overview of the RosterApp Application 120 The PlayerEJB Code 121 Entity Bean Class122 Local Home Interface126 Local Interface127A Guided Tour of the RosterApp Settings 128 RosterApp128 RosterClient129 RosterJAR130 TeamJAR131 Method Invocations in RosterApp 136 Creating a Player137 Adding a Player to a Team138 Removing a Player139 Dropping a Player from a Team140 Getting the Players of a Team141 Getting a Copy of a Team s Players142 Finding the Players by Position144 Getting the Sports of a Player145 Running the RosterApp Example 147 Setting Up147 Deploying the Application147 Running the Client148 CONTENTS ixdeploytool Tips for Entity Beans withContainer-Managed Persistence 148 Specifying the Bean s Type148 Selecting the Persistent Fields and Abstract Schema Name149
7 Defining EJB QL Queries for Finder and Select Methods149 Generating SQL and Specifying Table Creation149 Specifying the Database JNDI Name, User Name,and Password150 Defining Relationships150 Primary Keys for Container-Managed Persistence 151 The Primary Key Class151 Primary Keys in the Entity Bean Class152 Generating Primary Key Values153 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 Method158 The ejbCreate and ejbRemove Methods159 Running the SimpleMessageEJB Example 159 Starting the J2EE Server159 Creating the Queue159 Deploying the Application159 Running the Client160deploytool Tips for Message-Driven Beans 160 Specifying the Bean s Type and Transaction Management161 Setting the Message-Driven Bean s Characteristics161deploytool Tips for JMS Clients 162 Setting the Resource References162 Setting the Resource Environment References163 Specifying the JNDI Names163 Chapter 8: Enterprise JavaBeans Query Language.
8 165 Terminology 166 Simplified Syntax 167xCONTENTSE xample Queries 167 Simple Finder Queries167 Finder Queries That Navigate to Related Beans169 Finder Queries with Other Conditional Expressions170 Select Queries172 Full Syntax 173 BNF Symbols173 BNF Grammar of EJB QL173 FROM Clause176 Path Expressions179 WHERE Clause182 SELECT Clause190 EJB QL Restrictions 191 Chapter 9: Web Clients and Components..193 Web Client Life Cycle 194 Web Application Archives 196 Creating a WAR File197 Adding a WAR File to an EAR File197 Adding a Web Component to a WAR File198 Configuring Web Clients 199 Application-Level Configuration199 WAR-Level Configuration200 Component-Level Configuration202 Deploying Web Clients 203 Running Web Clients 203 Updating Web Clients 204 Internationalizing Web Clients 206 Chapter 10: java Servlet Technology.
9 209 What Is a Servlet? 210 The Example Servlets 211 Troubleshooting215 Servlet Life Cycle 216 Handling Servlet Life-Cycle Events216 Handling Errors218 Sharing Information 218 Using Scope Objects219 Controlling Concurrent Access to Shared Resources220 Accessing Databases221 Initializing a Servlet 222 CONTENTSxiWriting Service Methods 222 Getting Information from Requests223 Constructing Responses225 Filtering Requests and Responses 227 Programming Filters229 Programming Customized Requests and Responses230 Specifying Filter Mappings232 Invoking Other Web Resources 234 Including Other Resources in the Response234 Transferring Control to Another Web Component236 Accessing the Web Context 237 Maintaining Client State 238 Accessing a Session238 Associating Attributes with a Session238 Session
10 Management239 Session Tracking240 Finalizing a Servlet 241 Tracking Service Requests242 Notifying Methods to Shut Down242 Creating Polite Long-Running Methods243 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 Compilation253 Execution254 Initializing and Finalizing a JSP Page 256 Creating Static Content 257 Creating Dynamic Content 257 Using Objects within JSP Pages257 JSP Scripting Elements260 Including Content in a JSP Page 263 Transferring Control to Another Web Component 265 Param Element265 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?