Sams teach yourself j2 ee in 21 days

4,456 views
4,365 views

Published on

a

Published in: Career
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
4,456
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sams teach yourself j2 ee in 21 days

  1. 1. 00 0672323842 FM 3/20/02 9:31 AM Page i Martin Bond Dan Haywood Debbie Law Andy Longshaw Peter Roxburgh Teach Yourself J2EE in 21 Days 201 West 103rd St., Indianapolis, Indiana, 46290 USA
  2. 2. 00 0672323842 FM 3/20/02 9:31 AM Page ii Sams 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: 2001098579 00 4 3 2 1 Trademarks 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 Stephens MANAGING EDITOR Matt Purcell Michael Watson PROJECT EDITOR Christina Smith COPY EDITOR Pat Kinyon INDEXERS PROOFREADER TECHNICAL EDITOR First Printing: April, 2002 01 DEVELOPMENT EDITOR Melissa Lynch Printed in the United States of America 02 Todd Green Tom Dinse Erika Millen International Standard Book Number: 0-672-32384-2 03 ACQUISITIONS EDITOR Harold Finz, Steve Heckler, Farooq Karim, and Ari Krupnikov TEAM COORDINATOR Pamalee Nelson INTERIOR DESIGNER Gary Adair COVER DESIGNER Aren Howell PRODUCTION Cheryl Lynch Michelle Mitchell
  3. 3. 00 0672323842 FM 3/20/02 9:31 AM Page iii Contents at a Glance Introduction 1 WEEK 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 QL 7 9 27 81 125 165 211 271 WEEK 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 Libraries 333 335 395 429 461 501 555 603 WEEK 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 Services 651 653 701 741 787 827 869 923 Appendixes Appendix A An Introduction to UML B SQL Reference C An Overview of XML D The Java Community Process Glossary Index 965 977 987 999 1003 1025
  4. 4. 00 0672323842 FM 3/20/02 9:31 AM Page iv Contents Introduction 1 WEEK 1 Introducing J2EE and EJBs DAY 1 The Challenge of N-Tier Development 7 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
  5. 5. 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
  6. 6. 00 0672323842 FM vi 3/20/02 9:31 AM Page 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
  7. 7. 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
  8. 8. 00 0672323842 FM viii 3/20/02 9:31 AM Page viii Sams Teach Yourself J2EE in 21 Days Summary ............................................................................................................161 Q&A ....................................................................................................................161 Exercises ............................................................................................................162 DAY 5 Session EJBs 165 Overview ............................................................................................................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 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
  9. 9. 00 0672323842 FM 3/20/02 9:31 AM Page ix Contents ix The 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 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 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 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
  10. 10. 00 0672323842 FM x 3/20/02 9:31 AM Page 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 entity Element ......................................................................................313 The relationships Element ........................................................................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 DAY 8 Transactions and Persistence 333 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
  11. 11. 00 0672323842 FM 3/20/02 9:31 AM Page xi Contents xi What 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 Service 395 Messaging ..........................................................................................................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. 12. 00 0672323842 FM xii 3/20/02 9:31 AM Page xii Sams Teach Yourself J2EE in 21 Days Point-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 Beans 429 What Are Message-Driven Beans? ....................................................................430 The Message Producer’s 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 1—Sender Helper Class ......................................................................447 Step 2—Agency and Register Session Bean ................................................449 Step 3—The Message-Driven Bean ............................................................451 Step 4—Create the JMS Queue ....................................................................456 Step 5—Deploy the EJBS ............................................................................456 Step 6—Testing the ApplicantMatch Bean ..................................................457 Using Other Architectures ..................................................................................457 Summary ............................................................................................................458 Q&A ....................................................................................................................458 Exercise ..............................................................................................................458
  13. 13. 00 0672323842 FM 3/20/02 9:31 AM Page xiii Contents DAY 11 JavaMail xiii 461 Understanding 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 Servlets 501 The 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. 14. 00 0672323842 FM xiv 3/20/02 9:31 AM Page xiv Sams Teach Yourself J2EE in 21 Days Using 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 Pages 555 What 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. 15. 00 0672323842 FM 3/20/02 9:31 AM Page xv Contents xv JSP 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 Libraries 603 The 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. 16. 00 0672323842 FM xvi 3/20/02 9:31 AM Page xvi Sams Teach Yourself J2EE in 21 Days Processing 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 ..............................................................................................................648 WEEK 3 Integrating J2EE into the Enterprise DAY 15 Security 651 653 Security 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. 17. 00 0672323842 FM 3/20/02 9:31 AM Page xvii Contents xvii Summary ............................................................................................................698 Q&A ....................................................................................................................699 Exercises ............................................................................................................699 DAY 16 Integrating XML wi