ebook img

Sams Teach Yourself J2Ee in 21 Days: With Ejb, Jsp, Servlets, Jndi, Jdbc, and Xml (Sams Teach Yourself...in 21 Days) PDF

1128 Pages·2002·11.13 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Sams Teach Yourself J2Ee in 21 Days: With Ejb, Jsp, Servlets, Jndi, Jdbc, and Xml (Sams Teach Yourself...in 21 Days)

00 0672323842 FM 3/20/02 9:31 AM Page i Martin Bond Dan Haywood Debbie Law Andy Longshaw Peter Roxburgh Teach Yourself J2EE 21 in Days 201 West 103rd St.,Indianapolis,Indiana,46290 USA 00 0672323842 FM 3/20/02 9:31 AM Page ii Sams Teach Yourself J2EE in 21 Days ACQUISITIONSEDITOR Todd Green Copyright  2002 by Sams Publishing DEVELOPMENTEDITOR All rights reserved. No part of this book shall be reproduced,stored in a Michael Watson retrieval system,or transmitted by any means,electronic,mechanical,photo- copying,recording,or otherwise,without written permission from the publish- PROJECTEDITOR er. No patent liability is assumed with respect to the use of the information Christina Smith contained herein. Although every precaution has been taken in the preparation COPYEDITOR of this book,the publisher and author assume no responsibility for errors or Pat Kinyon omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein. INDEXERS Tom Dinse International Standard Book Number:0-672-32384-2 Erika Millen Library of Congress Catalog Card Number:2001098579 PROOFREADER Printed in the United States of America Melissa Lynch First Printing:April,2002 TECHNICALEDITOR Harold Finz,Steve Heckler, 03 02 01 00 4 3 2 1 Farooq Karim,and Ari Krupnikov Trademarks TEAMCOORDINATOR All terms mentioned in this book that are known to be trademarks or service Pamalee Nelson marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be INTERIORDESIGNER regarded as affecting the validity of any trademark or service mark. Gary Adair Warning and Disclaimer COVERDESIGNER Aren Howell Every effort has been made to make this book as complete and as accurate as possible,but no warranty or fitness is implied. The information provided is on PRODUCTION an “as is”basis. The authors and the publisher shall have neither liability nor Cheryl Lynch responsibility to any person or entity with respect to any loss or damages aris- Michelle Mitchell ing from the information contained in this book. EXECUTIVEEDITOR Michael Stephens MANAGINGEDITOR Matt Purcell 00 0672323842 FM 3/20/02 9:31 AM Page iii Contents at a Glance Introduction 1 WEEK 1 Introducing J2EE and EJBs 7 Day 1 The Challenge of N-Tier Development 9 2 The J2EE Platform and Roles 27 3 Naming and Directory Services 81 4 Introduction to EJBs 125 5 Session EJBs 165 6 Entity EJBs 211 7 CMP and EJB QL 271 WEEK 2 Developing J2EE Applications 333 Day 8 Transactions and Persistence 335 9 Java Message Service 395 10 Message-Driven Beans 429 11 JavaMail 461 12 Servlets 501 13 JavaServer Pages 555 14 JSP Tag Libraries 603 WEEK 3 Integrating J2EE into the Enterprise 651 Day 15 Security 653 16 Integrating XML with J2EE 701 17 Transforming XML Documents 741 18 Patterns 787 19 Integrating with External Resources 827 20 Using RPC-Style Web Services with J2EE 869 21 Web Service Registries and Message-Style Web Services 923 Appendixes Appendix A An Introduction to UML 965 B SQL Reference 977 C An Overview of XML 987 D The Java Community Process 999 Glossary 1003 Index 1025 00 0672323842 FM 3/20/02 9:31 AM Page iv Contents Introduction 1 WEEK 1 Introducing J2EE and EJBs 7 DAY 1 The Challenge of N-Tier Development 9 Monolithic Development ......................................................................................10 Consequences of Monolithic Applications ......................................................10 The Move into the Second Tier ............................................................................11 Consequences of the 2-Tier Design ................................................................12 Complexity Simplified by Modularity ................................................................14 Component Technology ..................................................................................15 Benefits of Modularity ....................................................................................16 Benefits of the 3-Tier Scenario ............................................................................16 A Model for Enterprise Computing ....................................................................17 Lifecycle ..........................................................................................................18 Persistence ......................................................................................................18 Naming ............................................................................................................18 Transaction ......................................................................................................19 Java 2 Enterprise Edition (J2EE) ..........................................................................20 Components and Containers ............................................................................20 J2EE Standard Services ..................................................................................21 J2EE Blueprints ..............................................................................................23 J2EE Compatibility Test Suite ........................................................................24 The Future of J2EE ..............................................................................................25 Summary ..............................................................................................................25 Q&A ......................................................................................................................25 Exercises ..............................................................................................................26 DAY 2 The J2EE Platform and Roles 27 Revisiting the J2EE Platform ..............................................................................28 Using Sun Microsystems’J2EE SDK ..................................................................28 Installing J2EE SDK 1.3 ................................................................................29 Starting the J2EE Reference Implementation (RI) ........................................32 Troubleshooting J2EE and Cloudscape ..........................................................34 Closing Down J2EE RI and Cloudscape ........................................................37 Optional Software Used in this Book ............................................................37 Understanding Tiers and Components ..................................................................38 The Business Tier ..........................................................................................39 The Presentation Tier ......................................................................................44 00 0672323842 FM 3/20/02 9:31 AM Page v Components:Web-Centric ..............................................................................45 The Client Tier ................................................................................................49 Standalone Client ............................................................................................52 Understanding Containers ....................................................................................55 Understanding the Services Containers Supply to Components ..........................56 Hypertext Transfer Protocol (HTTP) ..............................................................57 HTTP over Secure Sockets Layer (HTTPS) ..................................................57 Java Database Connectivity (JDBC) ..............................................................57 Java Transaction API (JTA) ............................................................................58 Java Authentication and Authorization Service (JAAS) ................................58 Java API for XML Parsing (JAXP) ................................................................58 Java Naming and Directory Interface (JNDI) ................................................59 JavaBeans Activation Framework (JAF) ........................................................59 JavaMail ..........................................................................................................60 Java Message Service (JMS) ..........................................................................60 Java Interface Definition Language (Java IDL) ..............................................60 Remote Method Invocation over Internet Inter-Orb Protocol (RMI-IIOP) ....61 Connector Architecture ....................................................................................62 Introducing Platform Roles ..................................................................................62 J2EE Product Provider ....................................................................................63 Application Component Provider ....................................................................63 Application Assembler ....................................................................................63 Application Deployer ......................................................................................64 Systems Administrator ....................................................................................64 Tool Provider ..................................................................................................65 Future of J2EE tools ........................................................................................65 Packaging and Deploying J2EE Applications ......................................................66 J2EE applications ..........................................................................................67 Breaking Modules down into Components ....................................................68 Summary ..............................................................................................................70 Q&A ......................................................................................................................70 Exercises—Case Study ........................................................................................71 The Job Agency ..............................................................................................72 Using the Agency Case Study ........................................................................73 Practice Makes Perfect ....................................................................................75 The Case Study Directory on the CD-ROM ..................................................76 Installing the Case Study Database ................................................................76 DAY 3 Naming and Directory Services 81 Naming and Directory Services ............................................................................82 Why Use a Naming Service? ................................................................................82 What is JNDI? ......................................................................................................83 00 0672323842 FM 3/20/02 9:31 AM Page vi vi Sams Teach Yourself J2EE in 21 Days Common Naming Services ..................................................................................83 Naming Conventions ............................................................................................84 Using JNDI ..........................................................................................................85 Using Sun Microsystems’J2EE Reference Implementation ..........................85 Obtaining an Initial Context ................................................................................86 Initial Context Naming Exceptions ......................................................................86 Defining the JNDI Service ..............................................................................87 JNDI Properties Files ......................................................................................88 Application Properties ....................................................................................89 Applet Parameters ..........................................................................................90 Hard-Coded Properties ....................................................................................90 Binding JNDI Objects ..........................................................................................90 Binding Objects ..............................................................................................91 Binding Problems ............................................................................................91 Name Persistence ............................................................................................92 Rebinding Objects ..........................................................................................92 Unbinding Objects ..........................................................................................92 Renaming Objects ..........................................................................................93 JNDI Name Lookup ..............................................................................................93 Changing Contexts ..........................................................................................94 Narrowing RMI-IIOP Objects ........................................................................95 Contexts ................................................................................................................96 Listing Contexts ..............................................................................................96 Creating and Destroying Contexts ..................................................................98 More on JNDI Names ........................................................................................100 Special Characters ........................................................................................100 Composite and Compound Names ................................................................100 URLs ..............................................................................................................101 Attributes ............................................................................................................102 Overview of LDAP X.500 Names ................................................................102 Obtaining an LDAP Server ..........................................................................103 Using OpenLDAP ........................................................................................104 Configuring JNDI to use LDAP ....................................................................106 Testing the LDAP Server ..............................................................................107 Obtaining a Directory Context ......................................................................108 Reading Attributes ........................................................................................108 Searching for Objects ....................................................................................109 Manipulating Attributes ................................................................................112 More on Objects ................................................................................................114 Loading Classes from a Code Base ..............................................................114 Defining a Code Base ....................................................................................114 References ....................................................................................................117 00 0672323842 FM 3/20/02 9:31 AM Page vii Contents vii What Else Can JNDI Do? ..................................................................................120 JNDI Events ..................................................................................................120 Security ..........................................................................................................121 Summary ............................................................................................................122 Q&A ....................................................................................................................123 Exercise ..............................................................................................................124 DAY 4 Introduction to EJBs 125 What Is an EJB? ................................................................................................126 Beans,Clients,Containers,and Servers ........................................................126 The EJB Landscape ......................................................................................127 Discovering EJBs ..........................................................................................127 Types of EJB ................................................................................................128 Common Uses of EJBs ..................................................................................128 Why Use EJBs? ..................................................................................................129 Hiding Complexity ........................................................................................130 Separation of Business Logic from UI and Data Access ..............................130 Container Services ........................................................................................131 What’s in an EJB? ..............................................................................................132 The Business Interface ..................................................................................132 The Business Logic ......................................................................................134 Factory Information ......................................................................................140 Bean Metadata ..............................................................................................141 How Do I Create an EJB? ..................................................................................142 The Creation Mechanism ..............................................................................142 Caveats on Code Creation ............................................................................143 Create the Deployable Component ..............................................................143 How Do I Deploy an EJB? ................................................................................147 Plugging into the Container ..........................................................................147 Performing the Deployment ..........................................................................148 How Do I Use an EJB? ......................................................................................148 Discovery ......................................................................................................148 Retrieval and Use ..........................................................................................149 Disposing of the EJB ....................................................................................150 Running the Client ........................................................................................150 Deploying and Using an EJB in the J2EE Reference Implementation ..............151 Opening the Case Study EAR File ................................................................152 Examining the Case Study Application ........................................................154 Deploying the Case Study Application ........................................................156 Testing the Case Study Application ..............................................................158 Troubleshooting the Case Study Application ................................................160 00 0672323842 FM 3/20/02 9:31 AM Page viii viii Sams Teach Yourself J2EE in 21 Days Summary ............................................................................................................161 Q&A ....................................................................................................................161 Exercises ............................................................................................................162 DAY 5 Session EJBs 165 Overview ............................................................................................................165 The javax.ejbPackage for Session Beans ........................................................167 Stateless Session Bean Lifecycle ........................................................................168 Specifying a Stateless Session Bean ..................................................................172 Implementing a Stateless Session Bean ............................................................175 Implementing javax.ejb.SessionBean ........................................................175 Implementing the Home Interface Methods ................................................175 Implementing the Remote Interface Methods ..............................................177 Exceptions ....................................................................................................179 Configuring and Deploying a Stateless Session Bean ......................................180 Using deploytool..........................................................................................181 Structural Elements ......................................................................................182 Presentational Elements ................................................................................183 Session Element ............................................................................................184 Deploying the Enterprise Application ..........................................................193 Stateful Session Bean Lifecycle ........................................................................193 Specifying a Stateful Session Bean ....................................................................196 Implementing a Stateful Session Bean ..............................................................198 Passivation ....................................................................................................198 Timeouts ........................................................................................................199 Chaining State ..............................................................................................200 Configuring and Deploying a Stateful Session Bean ........................................200 Client’s View ......................................................................................................201 Patterns and Idioms ............................................................................................202 Business Interface ..........................................................................................203 Adapter ..........................................................................................................204 Coarse-Grained ..............................................................................................205 Gotchas ..............................................................................................................205 Summary ............................................................................................................206 Q&A ....................................................................................................................207 Exercises ............................................................................................................207 DAY 6 Entity EJBs 211 Overview ............................................................................................................211 The N-tier Architecture Revisited ................................................................212 Comparison with RDBMS Technology ........................................................213 Identifying Entities ........................................................................................214 00 0672323842 FM 3/20/02 9:31 AM Page ix Contents ix The javax.ejbPackage for Entity Beans ..........................................................216 Entity Bean Types ..............................................................................................217 Remote Versus Local Interfaces ........................................................................217 BMP Entity Bean Lifecycle ................................................................................219 Specifying a BMP Entity Bean ..........................................................................225 Local-Home Interface ....................................................................................225 Local Interface ..............................................................................................230 Implementing a BMP Entity Bean ....................................................................231 Implementing the Local-Home Interface Methods ......................................235 Implementing the Local Interface Methods ..................................................241 Generating IDs ..............................................................................................243 Granularity Revisited ....................................................................................245 Beware Those Finder Methods! ....................................................................245 EJB Container Performance Tuning ..............................................................247 Configuring and Deploying a BMP Entity Bean ..............................................248 Entity Element ..............................................................................................249 Client’s View ......................................................................................................252 Session Beans Revisited ....................................................................................254 Patterns and Idioms ............................................................................................258 Interfaces,Façades,and State ......................................................................258 Use Local Interfaces for Entity Beans ..........................................................258 Dependent Value Classes ..............................................................................259 Self-Encapsulate Fields ................................................................................261 Don’t Use Enumerationfor Finders ..............................................................262 Acquire Late,Release Early ..........................................................................262 Business Interface Revisited ........................................................................264 Gotchas ..............................................................................................................264 Summary ............................................................................................................265 Q&A ....................................................................................................................266 Exercises ............................................................................................................266 DAY 7 CMP and EJB QL 271 Overview of Container-Managed Persistence ....................................................271 N-tier Architecture (Revisited Again) and CMP Fields ................................273 A Quick Word about the Case Study Database ............................................276 CMP Entity Bean Lifecycle ................................................................................277 Container-Managed Relationships ......................................................................279 Relationship Types ........................................................................................280 Navigability ..................................................................................................282 cmr-fields ....................................................................................................282 Manipulating Relationships ..........................................................................286 00 0672323842 FM 3/20/02 9:31 AM Page x x Sams Teach Yourself J2EE in 21 Days EJB QL ..............................................................................................................291 Select Methods ..............................................................................................291 Syntax and Examples ....................................................................................293 Further Notes ................................................................................................300 Specifying a CMP Entity Bean ..........................................................................301 The Local-Home Interface ............................................................................301 The Local Interface ......................................................................................301 Implementing a CMP Entity Bean ....................................................................302 Implementing javax.ejb.EntityBean ..........................................................302 Implementing the Local-Home Interface Methods ......................................305 Finder Methods ..............................................................................................308 Implementing the Local Interface Methods ..................................................312 Configuring a CMP Entity Bean ........................................................................313 The entityElement ......................................................................................313 The relationshipsElement ........................................................................317 Deploying a CMP Entity Bean ..........................................................................322 Patterns and Idioms ............................................................................................323 Normalize/Denormalize Data in ejbLoad()/ejbStore() ............................323 Don’t Expose cmp-fields ............................................................................324 Don’t Expose cmr-fields ............................................................................325 Enforce Referential Integrity Through the Bean’s Interface ........................326 Use Select Methods to Implement Home Methods ......................................327 Gotchas ..............................................................................................................328 Summary ............................................................................................................329 Q&A ....................................................................................................................329 Exercises ............................................................................................................330 WEEK 2 Developing J2EE Applications 333 DAY 8 Transactions and Persistence 335 Overview of Transactions ..................................................................................336 Container-Managed Transaction Demarcation ..................................................338 Bean Managed Transaction Demarcation ..........................................................345 Motivation and Restrictions ..........................................................................345 Using the Java Transaction API ....................................................................345 Deploying a BMTD Bean ............................................................................349 Client-Demarcated Transactions ..................................................................350 Exceptions Revisited ....................................................................................350 Extended Stateful Session Bean Lifecycle ........................................................352 Transactions:Behind the Scenes ........................................................................354 Transaction Managers,Resource Managers,and 2PC ..................................354 The JTA API ..................................................................................................356

Description:
J2EE has become required knowledge for any serious Java developer, but learning this large and complex specification requires a substantial investment of time and energy. Sams Teach Yourself J2EE in 21 Days presents the enterprise Java architecture in accessible, easy-to-comprehend lessons, describi
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.