Sams teach yourself j2 ee in 21 days

1128
201 West 103rd St., Indianapolis, Indiana, 46290 USA Martin Bond Dan Haywood Debbie Law Andy Longshaw Peter Roxburgh J2EE in 21 Days Teach Yourself

description

a

Transcript of Sams teach yourself j2 ee in 21 days

  • 1. 00 0672323842 FM3/20/029:31 AMPage iMartin Bond Dan Haywood Debbie Law Andy Longshaw Peter RoxburghTeach YourselfJ2EE in21 Days201 West 103rd St., Indianapolis, Indiana, 46290 USA

2. 00 0672323842 FM3/20/029:31 AMPage iiSams Teach Yourself J2EE in 21 Days Copyright 2002 by Sams Publishing All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein.Library of Congress Catalog Card Number: 2001098579004321Trademarks All terms mentioned in this book that are known to be trademarks or service 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 regarded as affecting the validity of any trademark or service mark.Warning and Disclaimer 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 an as is basis. The authors and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book.EXECUTIVE EDITOR Michael StephensMANAGING EDITOR Matt PurcellMichael WatsonPROJECT EDITOR Christina SmithCOPY EDITOR Pat KinyonINDEXERSPROOFREADER TECHNICAL EDITORFirst Printing: April, 2002 01DEVELOPMENT EDITORMelissa LynchPrinted in the United States of America02Todd GreenTom Dinse Erika MillenInternational Standard Book Number: 0-672-32384-203ACQUISITIONS EDITORHarold Finz, Steve Heckler, Farooq Karim, and Ari KrupnikovTEAM COORDINATOR Pamalee NelsonINTERIOR DESIGNER Gary AdairCOVER DESIGNER Aren HowellPRODUCTION Cheryl Lynch Michelle Mitchell 3. 00 0672323842 FM3/20/029:31 AMPage iiiContents at a Glance Introduction1WEEK 1 Introducing J2EE and EJBs Day 1 The Challenge of N-Tier Development 2 The J2EE Platform and Roles 3 Naming and Directory Services 4 Introduction to EJBs 5 Session EJBs 6 Entity EJBs 7 CMP and EJB QL7 9 27 81 125 165 211 271WEEK 2 Developing J2EE Applications Day 8 Transactions and Persistence 9 Java Message Service 10 Message-Driven Beans 11 JavaMail 12 Servlets 13 JavaServer Pages 14 JSP Tag Libraries333 335 395 429 461 501 555 603WEEK 3 Integrating J2EE into the Enterprise Day 15 Security 16 Integrating XML with J2EE 17 Transforming XML Documents 18 Patterns 19 Integrating with External Resources 20 Using RPC-Style Web Services with J2EE 21 Web Service Registries and Message-Style Web Services651 653 701 741 787 827 869 923Appendixes Appendix A An Introduction to UML B SQL Reference C An Overview of XML D The Java Community Process Glossary Index965 977 987 999 1003 1025 4. 00 0672323842 FM3/20/029:31 AMPage ivContents Introduction1WEEK 1 Introducing J2EE and EJBs DAY 1 The Challenge of N-Tier Development7 9Monolithic 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 Roles27Revisiting 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 5. 00 0672323842 FM3/20/029:31 AMPage vComponents: 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 ExercisesCase 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 Services81Naming and Directory Services ............................................................................82 Why Use a Naming Service? ................................................................................82 What is JNDI? ......................................................................................................83 6. 00 0672323842 FMvi3/20/029:31 AMPage viSams Teach Yourself J2EE in 21 DaysCommon 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 7. 00 0672323842 FM3/20/029:31 AMPage viiContentsviiWhat Else Can JNDI Do? ..................................................................................120 JNDI Events ..................................................................................................120 Security ..........................................................................................................121 Summary ............................................................................................................122 Q&A ....................................................................................................................123 Exercise ..............................................................................................................124 DAY 4 Introduction to EJBs125What 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 Whats 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 8. 00 0672323842 FMviii3/20/029:31 AMPage viiiSams Teach Yourself J2EE in 21 DaysSummary ............................................................................................................161 Q&A ....................................................................................................................161 Exercises ............................................................................................................162 DAY 5 Session EJBs165Overview ............................................................................................................165 The javax.ejb Package 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 Clients 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 EJBs211Overview ............................................................................................................211 The N-tier Architecture Revisited ................................................................212 Comparison with RDBMS Technology ........................................................213 Identifying Entities ........................................................................................214 9. 00 0672323842 FM3/20/029:31 AMPage ixContentsixThe javax.ejb Package 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 Clients View ......................................................................................................252 Session Beans Revisited ....................................................................................254 Patterns and Idioms ............................................................................................258 Interfaces, Faades, and State ......................................................................258 Use Local Interfaces for Entity Beans ..........................................................258 Dependent Value Classes ..............................................................................259 Self-Encapsulate Fields ................................................................................261 Dont Use Enumeration for 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 QL271Overview 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 10. 00 0672323842 FMx3/20/029:31 AMPage xSams Teach Yourself J2EE in 21 DaysEJB 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 entity Element ......................................................................................313 The relationships Element ........................................................................317 Deploying a CMP Entity Bean ..........................................................................322 Patterns and Idioms ............................................................................................323 Normalize/Denormalize Data in ejbLoad()/ejbStore() ............................323 Dont Expose cmp-fields ............................................................................324 Dont Expose cmr-fields ............................................................................325 Enforce Referential Integrity Through the Beans Interface ........................326 Use Select Methods to Implement Home Methods ......................................327 Gotchas ..............................................................................................................328 Summary ............................................................................................................329 Q&A ....................................................................................................................329 Exercises ............................................................................................................330WEEK 2 Developing J2EE Applications DAY 8 Transactions and Persistence333 335Overview 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 11. 00 0672323842 FM3/20/029:31 AMPage xiContentsxiWhat If It Goes Wrong? ................................................................................359 JTA Versus JTS ..............................................................................................361 Overview of Persistence Technologies ..............................................................363 JDBC ..................................................................................................................365 SQLj ....................................................................................................................367 SQLj Part 0 ....................................................................................................368 SQLj Part 1 ....................................................................................................373 SQLj Part 2 ....................................................................................................378 JDO ....................................................................................................................383 JDO Concepts ................................................................................................384 javax.jdo Classes and Interfaces ................................................................387 Queries ..........................................................................................................389 Other Features ..............................................................................................391 Gotchas ..............................................................................................................392 Summary ............................................................................................................393 Q&A ....................................................................................................................393 Exercises ............................................................................................................394 DAY 9 Java Message Service395Messaging ..........................................................................................................395 Message Passing ..........................................................................................396 Java Message Service API ..................................................................................397 JMS and J2EE ..............................................................................................398 JMS API Architecture ........................................................................................399 Message Domains ..........................................................................................400 Developing JMS Applications Using JBoss1 ....................................................402 JMS Implementation in JBoss ......................................................................402 Programming a JMS Application Using J2EE RI ..............................................404 J2EE RI Connection Factories ......................................................................404 Adding Destinations in J2EE RI ..................................................................404 Creating a Queue in J2EE RI ........................................................................404 Point-to-Point Messaging Example ....................................................................406 JMS Messages ..............................................................................................407 Creating a Message ......................................................................................409 Sending a Message ........................................................................................409 Closing the Connection ................................................................................410 Send JMS Text Message Example ................................................................410 Consuming Messages ....................................................................................411 Simple Synchronous Receiver Example ............................................................412 Receive JMS Text Message Example ..........................................................413 Asynchronous Messaging ............................................................................414 The Publish/Subscribe Message Domain ..........................................................415 12. 00 0672323842 FMxii3/20/029:31 AMPage xiiSams Teach Yourself J2EE in 21 DaysPoint-to-Point Messaging Example ....................................................................416 Bulletin Board Publisher ..............................................................................417 Bulletin Board Subscriber ............................................................................418 Creating Durable Subscriptions ....................................................................420 Additional JMS Features ..............................................................................422 Introduction to XML ..........................................................................................425 What Is XML and Why Would You Use It? ..............................................425 Summary ............................................................................................................426 Q&A ....................................................................................................................426 Exercise ..............................................................................................................427 DAY 10 Message-Driven Beans429What Are Message-Driven Beans? ....................................................................430 The Message Producers View ......................................................................430 Similarities and Differences with Other EJBs ..............................................431 Programming Interfaces in a Message-Driven Bean ..........................................431 Life Cycle of a Message-Driven Bean ..............................................................432 The Message-Driven Bean Context ..............................................................433 Creating a Message-Driven Bean ......................................................................434 Method-Ready Pool ......................................................................................434 The Demise of the Bean ................................................................................435 Consuming Messages ....................................................................................435 Handling Exceptions ....................................................................................436 Container- and Bean-Managed Transactions ..............................................436 Message Acknowledgment ............................................................................437 JMS Message Selectors ................................................................................438 Writing a Simple Message-Driven Bean ............................................................439 Implementing the Interfaces ........................................................................439 Running the Example ........................................................................................440 Creating the Queue ........................................................................................441 Deploying the Message-Driven Bean ............................................................442 Create a Sender Client to Create a Message ................................................445 Developing the Agency Case Study Example ..................................................447 Step 1Sender Helper Class ......................................................................447 Step 2Agency and Register Session Bean ................................................449 Step 3The Message-Driven Bean ............................................................451 Step 4Create the JMS Queue ....................................................................456 Step 5Deploy the EJBS ............................................................................456 Step 6Testing the ApplicantMatch Bean ..................................................457 Using Other Architectures ..................................................................................457 Summary ............................................................................................................458 Q&A ....................................................................................................................458 Exercise ..............................................................................................................458 13. 00 0672323842 FM3/20/029:31 AMPage xiiiContentsDAY 11 JavaMailxiii461Understanding E-Mail ........................................................................................462 SMTP ............................................................................................................463 Post Office Protocol 3 (POP3) ......................................................................463 Internet Message Access Protocol (IMAP) ..................................................464 Other Protocols ..............................................................................................464 Multipurpose Internet Mail Extensions (MIME) ..........................................464 Introducing the JavaMail API ............................................................................465 Setting up Your Development Environment ......................................................465 Sending a First E-mail ........................................................................................466 Creating a First E-mail ..................................................................................466 Creating Multi-Media E-mails ............................................................................472 Creating the Message: Approach #1 ............................................................472 Creating the Message: Approach #2 ............................................................476 Sending E-mails with Attachments ....................................................................482 Exploring the JavaMail API ................................................................................485 Retrieving Messages ......................................................................................485 Deleting Messages ........................................................................................489 Getting Attachments ......................................................................................490 Authenticating Users and Security ................................................................494 Summary ............................................................................................................497 Q&A ....................................................................................................................497 Exercises ............................................................................................................499 DAY 12 Servlets501The Purpose and Use of Servlets ......................................................................502 Tailored for Web Applications ....................................................................502 Server and Platform Independence ..............................................................503 Efficient and Scalable ....................................................................................503 Servlets Integration with the Server ..............................................................503 Introduction to HTTP ........................................................................................504 HTTP Structure ............................................................................................504 Other HTTP Methods ....................................................................................507 Server Responses ..........................................................................................507 Introduction to HTML ........................................................................................509 The Servlet Environment ....................................................................................513 Servlet Containers ........................................................................................513 The Servlet Class Hierarchy ..............................................................................513 Simple Servlet Example ....................................................................................514 Passing Parameter Data to a Servlet ..................................................................519 How to Access Parameters ............................................................................519 Servlet Example with Parameters ................................................................520 14. 00 0672323842 FMxiv3/20/029:31 AMPage xivSams Teach Yourself J2EE in 21 DaysUsing a POST Request ..................................................................................522 The Servlet Lifecycle ....................................................................................522 The Servlet Context ......................................................................................524 Web Applications ................................................................................................525 Web Application Files and Directory Structure ..........................................525 The Web Application Deployment Descriptor ..............................................526 Handling Errors ..................................................................................................528 HTTP Errors ..................................................................................................528 Servlet Exception Handling ..........................................................................529 Retaining Client and State Information ..............................................................530 Using Session Objects ..................................................................................530 Hidden Form Fields ......................................................................................532 Cookies ..........................................................................................................532 Creating a Cookie ........................................................................................533 URL Rewriting ..............................................................................................535 Servlet Filtering ..................................................................................................535 Programming Filters ......................................................................................535 Example Auditing Filter ................................................................................537 Deploying Filters ..........................................................................................538 Event Listening ..................................................................................................541 Deploying the Listener ..................................................................................543 Servlet Threads ..................................................................................................545 Security and the Servlet Sandbox ......................................................................546 Agency Case Study ............................................................................................546 AgencyTable Servlet Code ............................................................................546 Deploying the AgencyTable Servlet ..............................................................548 Summary ............................................................................................................552 Q&A ....................................................................................................................553 Exercises ............................................................................................................553 DAY 13 JavaServer Pages555What is a JSP? ....................................................................................................556 Separating Roles ............................................................................................557 Translation and Execution ............................................................................557 JSP Syntax and Structure ....................................................................................557 JSP Elements ................................................................................................558 First JSP example ..............................................................................................560 JSP Problems ................................................................................................563 JSP Lifecycle ......................................................................................................563 Detecting and Correcting JSP Errors ............................................................565 JSP Lifecycle Methods ..................................................................................569 15. 00 0672323842 FM3/20/029:31 AMPage xvContentsxvJSP Directives ....................................................................................................570 The include Directive ..................................................................................570 The page Directive ........................................................................................571 Accessing HTTP Servlet Variables ....................................................................575 Using HTTP Request Parameters ......................................................................576 Simplifying JSP pages with JavaBeans ..............................................................577 What Is a JavaBean? ......................................................................................578 Defining a JavaBean ......................................................................................579 Getting Bean Properties ................................................................................579 Setting Bean Properties ................................................................................580 Initializing Beans ..........................................................................................581 Using a Bean with the Agency Case Study ..................................................581 Adding a Web Interface to the Agency Case Study ..........................................585 Structure and Navigation ..............................................................................585 Look and Feel ................................................................................................588 Error Page Definition ....................................................................................595 Deploying the Case Study JSPs ....................................................................597 Comparing JSP with Servlets ............................................................................600 Summary ............................................................................................................601 Q&A ....................................................................................................................601 Exercise ..............................................................................................................602 DAY 14 JSP Tag Libraries603The Role of Tag Libraries ..................................................................................604 Developing a Simple Custom Tag ......................................................................605 Using a Simple Tag ......................................................................................605 The Tag Library Descriptor (TLD) ..............................................................606 Custom Java Tags ..........................................................................................608 The doStartTag() Method ............................................................................610 The Hello World Custom Tag ..................................................................611 Deploying a Tag Library Web Application ..................................................612 Defining the TLD Location ..........................................................................614 Using Simple Tags ........................................................................................614 Tags with Attributes ............................................................................................615 Tags that Define Script Variables ........................................................................618 Iterative Tags ......................................................................................................622 Co-operating Tags ..............................................................................................626 Using Shared Scripting Variables ..................................................................626 Hierarchical Tag Structures ..........................................................................627 Defining Tag Extra Info Objects ........................................................................634 Validating Attributes ......................................................................................635 Defining Scripting Variables ........................................................................637 16. 00 0672323842 FMxvi3/20/029:31 AMPage xviSams Teach Yourself J2EE in 21 DaysProcessing Tag Bodies ........................................................................................637 JavaServer Pages Standard Tag Library (JSPTL) ..............................................640 Using the JSPTL with the J2EE RI ..............................................................641 Using the JSPTL forEach Tag ....................................................................643 Other JSPTL Tags ........................................................................................645 JSPTL Scripting Language ............................................................................645 Other Jakarta Tag Libraries ..........................................................................646 Summary ............................................................................................................647 Q&A ....................................................................................................................647 Exercise ..............................................................................................................648WEEK 3 Integrating J2EE into the Enterprise DAY 15 Security651 653Security Overview ..............................................................................................654 Security Terminology ....................................................................................654 Common Security Technology ..........................................................................656 Symmetric Encryption ..................................................................................656 Asymmetric Encryption ................................................................................658 SSL and HTTPS ............................................................................................659 Checksums and Digests ................................................................................660 Digital Certificates ........................................................................................660 Security in J2EE ................................................................................................661 J2EE Security Terminology ..........................................................................661 Working with J2EE RI Security ....................................................................663 Security and EJBs ..............................................................................................666 Defining EJB Security ..................................................................................666 Defining Roles ..............................................................................................666 Defining the Security Identity ......................................................................668 Defining Method Permissions ......................................................................670 Mapping Principals to Roles ........................................................................674 Using Roles as the Security Identity ............................................................676 Security in Web Applications and Components ................................................682 Web Authentication ......................................................................................683 Configuring J2EE RI Basic Authentication ..................................................684 Declarative Web Authorization ....................................................................685 Programmatic Web Authorization ................................................................691 Adding Programmatic Web Security to the Case Study ..............................692 Using Secure Web Authentication Schemes ................................................694 Security and JNDI ............................................................................................695 Simple LDAP Authentication ........................................................................696 SASL Authentication ....................................................................................696 17. 00 0672323842 FM3/20/029:31 AMPage xviiContentsxviiSummary ............................................................................................................698 Q&A ....................................................................................................................699 Exercises ............................................................................................................699 DAY 16 Integrating XML with J2EE701The Drive to Platform-Independent Data Exchange ..........................................702 Benefits and Characteristics of XML ............................................................703 Origins of XML ..................................................................................................703 Structure and Syntax of XML ............................................................................704 HTML and XML ..........................................................................................705 Structure of an XML Document ..................................................................705 Declarations ..................................................................................................706 Elements ........................................................................................................706 Well-Formed XML Documents ....................................................................708 Attributes ......................................................................................................708 Comments ......................................................................................................709 Creating Valid XML ..........................................................................................710 Document Type Definitions ..........................................................................710 Namespaces ..................................................................................................714 Enforcing Document Structure with an XML Schema ................................715 How XML Is Used in J2EE ..........................................................................718 Parsing XML ......................................................................................................718 The JAXP Packages ..........................................................................................720 Parsing XML using SAX ....................................................................................720 Document Object Model (DOM) Parser ..........................................................725 Modifying a DOM Tree ......................................................................................731 Java Architecture for XML Binding ..................................................................732 Differences Between JAXP and JAXB ........................................................733 Extending the Agency Case Study ....................................................................734 Step 1Change Session Beans ....................................................................735 Step 2Amend the MessageSender Helper Class ........................................736 Step 3Amend the ApplicantMatch Message-Driven Bean ......................737 Summary ............................................................................................................739 Q&A ....................................................................................................................739 Exercises ............................................................................................................740 DAY 17 Transforming XML Documents741Presenting XML to Clients ................................................................................742 Presenting XML to Browsers ........................................................................743 Extensible Stylesheet Language (XSL) ........................................................744 XSL-FO XSL Formatting Objects ................................................................744 18. 00 0672323842 FMxviii3/20/029:31 AMPage xviiiSams Teach Yourself J2EE in 21 DaysExtensible Stylesheet Transformations (XSLT) ................................................745 Applying Stylesheets ....................................................................................746 Storing Transformed Documents on the Server ............................................746 Presenting XML Documents and Stylesheets to the Client ..........................747 Transforming the XML Document on the Server ........................................747 Using XALAN with J2EE ..................................................................................748 Transforming XML Documents with XALAN ............................................749 Using XALAN from the Command Line ....................................................750 Using XSLT in Java Applications ....................................................................751 XSLT Stylesheets ................................................................................................755 Template Rules ..............................................................................................756 Text Representation of XML Elements ......................................................761 Using XPath with XSLT ................................................................................762 Default Stylesheet Rules ..............................................................................764 Processing Attributes ....................................................................................765 Using Stylesheet Elements ................................................................................767 Processing Whitespace and Text ..................................................................767 Adding Comments ........................................................................................769 Attribute Values ............................................................................................770 Creating and Copying Elements ....................................................................771 Attributes and Attribute Sets ........................................................................774 Additional XSL Elements ............................................................................777 XSLT Compilers ................................................................................................780 Summary ............................................................................................................781 Q&A ....................................................................................................................782 Exercises ............................................................................................................782 DAY 18 Patterns787J2EE Patterns ......................................................................................................788 What Are Patterns? ........................................................................................788 Why Use Patterns? ........................................................................................790 Types of Patterns ..........................................................................................790 J2EE Patterns ................................................................................................791 Pattern Catalogs ............................................................................................792 Applying J2EE-Specific Patterns ......................................................................792 Applying Patterns in a Context ....................................................................793 Generic Patterns ............................................................................................794 J2EE Presentation-Tier Patterns ....................................................................795 J2EE Business-Tier Patterns ..........................................................................795 J2EE Integration-Tier Patterns ......................................................................796 Patterns Within J2EE ....................................................................................797 19. 00 0672323842 FM3/20/029:31 AMPage xixContentsxixPatterns in Context ..............................................................................................797 Analysing the Case Study ............................................................................797 Session Facades and Entity EJBs ..................................................................798 Data Exchange and Value Objects ................................................................800 Data Access Without Entity EJBs ................................................................804 Messages and Asynchronous Activation ......................................................811 Composing an Entity ....................................................................................812 Composing a JSP ..........................................................................................813 JSPs and Separation of Concerns ..................................................................817 Client-Side Proxies and Delegates ................................................................820 Locating Services ..........................................................................................821 Any Other Business ......................................................................................822 Refactoring the Case Study ..........................................................................822 Directions for J2EE Patterns ..............................................................................823 Summary ............................................................................................................824 Q & A ..................................................................................................................824 Exercises ............................................................................................................825 DAY 19 Integrating with External Resources827Reviewing External Resources and Legacy Systems ........................................828 Introducing Connector Architecture ..................................................................829 Overview of the Architecture ........................................................................829 Roles and Responsibilities ............................................................................830 Using the Common Client Interface ..................................................................834 Interacting with an EIS ..................................................................................834 Installing a Resource Adapter ......................................................................835 Creating a First CCI Application ..................................................................836 Managing Transactions and Exploring Records ..........................................843 Introducing Other Connectivity Technologies ....................................................848 Introducing CORBA ..........................................................................................849 Introducing Java IDL ..........................................................................................851 Using RMI over IIOP ........................................................................................851 RMI over JRMP Example ............................................................................852 RMI over IIOP Example ..............................................................................857 Introducing JNI ..................................................................................................860 Evaluation of Integration Technologies ..............................................................865 Summary ............................................................................................................865 Q&A ....................................................................................................................866 Exercises ............................................................................................................867 DAY 20 Using RPC-Style Web Services with J2EE869Web Service Overview ......................................................................................870 What Is a Web Service? ................................................................................870 Why Use Web Services? ..............................................................................872 20. 00 0672323842 FMxx3/20/029:31 AMPage xxSams Teach Yourself J2EE in 21 DaysWeb Service Technologies and Protocols ....................................................873 Web Service Architecture ..............................................................................873 Web Services for J2EE ......................................................................................875 J2EE Web Service Architecture ....................................................................875 Tools and Technologies ................................................................................876 Integrating Web Services with Existing J2EE Components ........................878 Using an RPC-style SOAP-Based Web Service ................................................879 RPC-Oriented Web Services ........................................................................880 Setting up Axis under Tomcat 4.0 ................................................................881 Service Description Information ..................................................................883 Anatomy of a WSDL Document ..................................................................883 Creating a Java Proxy from WSDL ..............................................................885 Calling the Web Service Through SOAP ......................................................889 A Half-Way House ........................................................................................891 Debugging a SOAP Interaction ....................................................................892 Implementing an RPC-Style SOAP-Based Web Service ..................................894 Wrapping up a Java class as a Web Service ..................................................894 A Client for Your Web Service ......................................................................898 Starting from WSDL ....................................................................................900 Using Axis JWS files ....................................................................................903 Session Context and Web Services ..............................................................905 Wrapping Existing J2EE Functionality as Web Services ............................909 Parameter Types and Type Mapping ..................................................................911 Mapping Between Java and SOAP/WSDL Types ........................................911 Mapping Complex Types with Serializers ....................................................912 Going Further with Complex Type Mapping ................................................919 Summary ............................................................................................................919 Q&A ....................................................................................................................920 Exercises ............................................................................................................921 DAY 21 Web Service Registries and Message-Style Web Services923Registries for Web Services ................................................................................924 What is a Web Service Registry? ..................................................................924 Why Do I Need One? ....................................................................................924 How Do They Work? ....................................................................................925 Types of Registry ..........................................................................................925 ebXML Registry and Repository ..................................................................926 UDDI Overview ............................................................................................928 Accessing Information in a UDDI Registry ......................................................929 Manipulating Service Information using UDDI4J ........................................929 Manipulating Service Information Using the IBM WSTK Client API ........932 Retrieving and Using Service Information ..................................................933 21. 00 0672323842 FM3/20/029:31 AMPage xxiContentsxxiUsing JAXR for Registry Access ......................................................................934 A Generic Approach ......................................................................................934 Using JAXR to Store and Retrieve Service Information ..............................936 Using a Message-Based SOAP Interface ..........................................................937 Message-Style Versus RPC-style ..................................................................937 Creating a Client ............................................................................................938 Creating a Service ........................................................................................939 Sending and Receiving SOAP Messages with JAXM ......................................939 JAXM and J2EE ............................................................................................941 Configuring JAXM ........................................................................................941 Sending Basic SOAP Messages ....................................................................942 Running the Simple Client ............................................................................947 Populating the Message ................................................................................947 Headers and Attachments ..............................................................................951 Receiving a SOAP Message Using JAXM ..................................................952 Using a JAXM Profile ..................................................................................955 Sending a Message Using a JAXM Profile ..................................................957 Receiving a Message Using a JAXM Profile ................................................959 Summary ............................................................................................................962 Q&A ....................................................................................................................962 Exercises ............................................................................................................963 Appendix A An Introduction to UML965Introducing the UML ..........................................................................................965 Use Case Diagrams ............................................................................................967 Class Diagrams ..................................................................................................969 Associations ..................................................................................................969 Attributes ......................................................................................................970 Operations ......................................................................................................971 Generalization ................................................................................................972 Constraints ....................................................................................................973 Sequence Diagrams ............................................................................................973 Appendix B SQL Reference977Commonly Used SQL Statements (SQL99) ......................................................978 ALTER TABLE ..................................................................................................978 CREATE TABLE..................................................................................................979 CREATE VIEW ..................................................................................................979 DELETE ............................................................................................................980 DROP TABLE ....................................................................................................980 DROP VIEW ......................................................................................................980 INSERT ............................................................................................................980 22. 00 0672323842 FM3/20/029:31 AMPage xxii............................................................................................................981 ............................................................................................................983 Commonly Used SQL Clauses ..........................................................................983 FROM ................................................................................................................983 WHERE ..............................................................................................................983 GROUP BY ........................................................................................................984 HAVING ............................................................................................................984 ORDER BY ........................................................................................................984 SELECT UPDATEAppendix C An Overview of XML987What Is XML? ....................................................................................................988 Elements ........................................................................................................988 Declarations ..................................................................................................989 Comments ......................................................................................................990 Special Characters ........................................................................................990 Namespaces ..................................................................................................991 Enforcing XML Document Structure ................................................................991 Document Type Definition (DTD) ................................................................992 XML Schema ................................................................................................995 Where to Find More Information ......................................................................997 Appendix D The Java Community Process999Introducing the JCP ............................................................................................999 Getting Involved ..............................................................................................1000 JCP Members ..............................................................................................1000 Expert Groups ..............................................................................................1000 The Public ....................................................................................................1000 Process Management Office (PMO) ..........................................................1001 Executive Committees ................................................................................1001 Understanding the JSR Process ........................................................................1001 Taking the Next Step ........................................................................................1002 Glossary1003Index1025 23. 00 0672323842 FM3/20/029:31 AMPage xxiiiAbout the Authors The authors of this book work for Content Master Ltd., a technical authoring company in the United Kingdom specializing in the production of training and educational materials. For more information on Content Master, please see its Web site at www.contentmaster.com. Martin Bond, B.Sc. M.Sc. C.Eng, M.B.C.S., was born near Manchester England in 1958. Martin left a budding academic career to develop parallel processing compilers for Inmos. Martin has designed and developed systems using C++, Java, and JavaScript and has developed training courses on Unix programming, Solaris security, Java programming, and XML. Martin has an honors degree and a masters degree in computer science from Aberystwyth, Wales, and is a European chartered engineer. Martin currently works as an IT trainer and consultant based in Cornwall, England. Dan Haywood has been working on large and small software development projects for more than 12 years. These days, he fills his days with consulting, training and technical writing, specializing in OO design, Java and J2EE, Sybase technical consulting, and data modeling. Previously, Dan worked at Sybase Professional Services, performing a variety of roles, mostly in the financial industry, including architect, performance specialist, and project manager. Dan started his IT career at (what was then) Andersen Consulting, working as a developer on large-scale projects in government and in utilities. Dan is married and has a baby daughter. Debbie Law B.Sc., was born in Romsey, England in 1959. Debbie started on compiler development for parallel processing systems, later working on the design and development of client server applications. As a technical manager for Siemens, she was one of a small group of select staff on an intensive learning program studying worldwide business practices, including several weeks at MIT and Harvard. Debbie has an honors degree in computer science from Southampton, England and currently works as an IT consultant based in Cornwall, England. Andy Longshaw is a consultant, writer, and educator specializing in J2EE, XML, and Web-based technologies and components, particularly the design and architecture decisions required to use these technologies successfully. Andy has been explaining technology for most of the last decade as a trainer and in conference sessions. A wild rumor suggests that some people have managed to stay awake in these sessions. Despite being well educated and otherwise fairly normal, Andy still subjects himself and his family to trial by unpredictability by watching Manchester City FC far more often than is healthy. 24. 00 0672323842 FM3/20/029:31 AMPage xxivPeter Roxburgh graduated with a first class degree with honors in business, and has since followed a diverse career path. From his home in the medieval walled town of Conwy, North Wales, he authors a wide-variety of training courses, and books including Building .NET Applications for Mobile Devices (Microsoft Press, 2002). He has also written and contributed to a number of journals and Web sites on cutting-edge technologies. Peter spends his spare time playing guitar and bouldering on nearby sea cliffs and mountain crags. When he is not strumming or risking life and limb, he enjoys spending relaxing and quality time with his daughter, Chloe. 25. 00 0672323842 FM3/20/029:31 AMPage xxvDedication To Sarah, for encouragement, advice, and regular supplies of flapjacks; and to Adam and Josh, for providing me with a life that doesnt revolve around computers. AL To Sue: Thank you for all these happy years. Love, Dan.Acknowledgments The authors would like to thank the various project managers and editors involved in this book, without whom it would never have seen the light of day. Special thanks go to Suzanne Carlino at Content Master and Todd Green, Michael Watson, Christy Franklin, and the editing team at SAMS. We would also like to acknowledge the work of Alex Ferris and John Sharp in the initial phases of this project. 26. 00 0672323842 FM3/20/029:31 AMPage xxviTell Us What You Think! As the reader of this book, you are our most important critic and commentator. We value your opinion and want to know what were doing right, what we could do better, what areas youd like to see us publish in, and any other words of wisdom youre willing to pass our way. As an executive editor for Sams Publishing, I welcome your comments. You can fax, e-mail, or write me directly to let me know what you did or didnt like about this book as well as what we can do to make our books stronger. Please note that I cannot help you with technical problems related to the topic of this book, and that due to the high volume of mail I receive, I might not be able to reply to every message. When you write, please be sure to include this books title and author as well as your name and phone or fax number. I will carefully review your comments and share them with the author and editors who worked on the book. Fax:317-581-4770E-mail:[email protected]:Michael Stephens Executive Editor Sams Publishing 201 West 103rd Street Indianapolis, IN 46290 USA 27. 01 0672323842 Intro3/20/029:23 AMPage 1Introduction The world has come a long way since Duke first started tumbling in early versions of Netscape Navigator. Java has outgrown its humble origins as a cool way of providing interactivity on Web pages and has found a new role as a major, server-side development platform. The actual Java language has changed little in the intervening years, but an enterprise-quality infrastructure has risen up around it. This infrastructure, Java 2 Enterprise Edition or J2EE for short, allows Java developers to create sophisticated and powerful enterprise applications that provide mission-critical functionality for many thousands of users. Unlike competing platforms, such as Microsoft .NET, J2EE is a specification rather than a product. The capabilities and functionality of each release of J2EE is agreed on through the Java Community Process (JCP). The platform is then implemented by application server vendors and producers, such as BEA, IBM, iPlanet, ATG, SilverStream, and JBOSS. This means that J2EE developers have a choice of product vendors from whom to select, based on quality, support, or ease of use. The ability to submit technologies through the JCP, and the two-way flow that exists between the main Java