Advanced Java 2 Platform HOW TO PROGRAM Deitel ™ Books, Cyber Classrooms, Complete Training Courses and Web-Based Training published by Prentice Hall How to Program Series Advanced Java ™ 2 Platform How to Program C How to Program, 3/E C++ How to Program, 3/E C# How to Program e-Business and e-Commerce How to Program Internet and World Wide Web How to Program, 2/E Java ™ How to Program, 4/E Perl How to Program Visual Basic ® 6 How to Program Visual Basic ® .NET How to Program Visual C++ ® .NET How to Program Wireless Internet & Mobile Business How to Program XML How to Program Multimedia Cyber Classroom and Web-Based Training Series (for information regarding Deitel™ Web-based training visit www.ptgtraining.com) Advanced Java ™ 2 Platform Multimedia Cyber Classroom C++ Multimedia Cyber Classroom, 3/E C# Multimedia Cyber Classroom, 3/E e-Business and e-Commerce Multimedia Cyber Classroom Internet and World Wide Web Multimedia Cyber Classroom, 2/E Java ™ 2 Multimedia Cyber Classroom, 4/E Perl Multimedia Cyber Classroom Visual Basic ® 6 Multimedia Cyber Classroom Visual Basic ® .NET Multimedia Cyber Classroom Visual C++ ® .NET Multimedia Cyber Classroom Wireless Internet & Mobile Business Programming Multimedia Cyber Classroom XML Multimedia Cyber Classroom The Complete Training Course Series The Complete Advanced Java ™ 2 Platform Training Course The Complete C++ Training Course, 3/E The Complete C# Training Course, 3/E The Complete e-Business and e-Commerce Programming Training Course The Complete Internet and World Wide Web Programming Training Course The Complete Java ™ 2 Training Course, 3/E The Complete Perl Training Course The Complete Visual Basic ® 6 Training Course The Complete Visual Basic ® .NET Training Course The Complete Visual C++ ® .NET Training Course The Complete Wireless Internet & Mobile Business Programming Training Course The Complete XML Training Course .NET Series C# How to Program Visual Basic ® .NET How to Program Visual C++ ® .NET How to Program Visual Studio ® Series Getting Started with Microsoft ® Visual C++ ™ 6 with an Introduction to MFC Visual Basic ® 6 How to Program C# How to Program Visual Basic ® .NET How to Program Visual C++ ® .NET How to Program For Managers Series e-Business and e-Commerce for Managers Coming Soon e-books and e-whitepapers To communicate with the authors, send email to: deitel@deitel.com For information on corporate on-site seminars and public seminars offered by Deitel & Associates, Inc. worldwide, visit: www.deitel.com For continuing updates on Prentice Hall and Deitel & Associates, Inc. publications visit the Prentice Hall Web site www.prenhall.com/deitel Advanced Java 2 Platform HOW TO PROGRAM H. M. Deitel Deitel & Associates, Inc. P. J. Deitel Deitel & Associates, Inc. S. E. Santry Deitel & Associates, Inc. PRENTICE HALL, Upper Saddle River, New Jersey 07458 Library of Congress Cataloging-in-Publication Data on File Vice President and Editorial Director: Marcia Horton Acquisitions Editor: Petra J. Recter Assistant Editor: Sarah Burrows Project Manager: Crissy Statuto Editorial Assistant: Karen Schultz Production Editor: Camille Trentacoste Managing Editor: David A. George Executive Managing Editor: Vince O’Brien Chapter Opener and Cover Designer: Tamara Newnam Cavallo Art Director: Heather Scott Marketing Manager: Jennie Burger Manufacturing Buyer: Pat Brown Manufacturing Manager: Trudy Pisciotti Assistant Vice President of Production and Manufacturing: David W. Riccardi © 2001 by Prentice-Hall, Inc. Upper Saddle River, New Jersey 07458 The authors and publisher of this book have used their best efforts in preparing this book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the docu- mentation contained in this book. The authors and publisher shall not be liable in any event for incidental or con- sequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trade- marks and registered trademarks. Where those designations appear in this book, and Prentice Hall and the authors were aware of a trademark claim, the designations have been printed in initial caps or all caps. All product names mentioned remain trademarks or registered trademarks of their respective owners. All rights reserved. No part of this book may be reproduced, in any form or by any means, without permission in writing from the publisher. Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 ISBN 0-13-034151-7 Prentice-Hall International (UK) Limited, London Prentice-Hall of Australia Pty. Limited, Sydney Prentice-Hall Canada Inc., Toronto Prentice-Hall Hispanoamericana, S.A., Mexico Prentice-Hall of India Private Limited, New Delhi Prentice-Hall of Japan, Inc., Tokyo Pearson Education Asia Pte. Ltd., Singapore Editora Prentice-Hall do Brasil, Ltda., Rio de Janeiro In loving memory of our Uncle and Granduncle Joseph Deitel: “His pleasure was giving.” Harvey and Paul Deitel For my brother Tim, who, by his example, always has challenged me to excel. Sean Trademarks Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. Prentice Hall is independent of Sun Microsystems, Inc. Copyright © 2000 Microsoft Corporation, One Microsoft Way, Redmond, Washington 98052-6399 U.S.A. All rights reserved. Netscape Communicator browser window© 1999 Netscape Communications Corporation. Used with permission. Netscape Communications has not authorized, sponsored, endorsed, or approved this publication and is not responsible for its content. Openwave, the Openwave logo, and UP.SDK are trademarks of Openwave Systems Inc. All rightsreserved." Palm OS, Palm Computing, HandFAX, HandSTAMP, HandWEB, Graffiti, HotSync, iMEssenger, MultiMail, Palm.Net, PalmConnect, PalmGlove, PalmModem, PalmPoint, PalmPrint, PalmSource, and the Palm Platform Compatible Logo are registered trademarks of Palm, Inc. Palm, the Palm logo, MyPalm, PalmGear, PalmPix, PalmPower, AnyDay, EventClub, HandMAIL, the HotSync Logo, PalmGlove, Palm Powered, the Palm trade dress, Smartcode, Simply Palm, We Sync and Wireless Refresh are trademarks of Palm, Inc. Contents Preface xxi 1 Introduction 1 1.1 Introduction 2 1.2 Architecture of the Book 3 1.2.1 Advanced GUI, Graphics and JavaBeans 3 1.2.2 Distributed Systems 4 1.2.3 Web Services 5 1.2.4 Enterprise Java 5 1.2.5 Enterprise Case Study 6 1.2.6 XML 7 1.3 Tour of the Book 7 1.4 Running Example Code 18 1.5 Design Patterns 19 1.5.1 History of Object-Oriented Design Patterns 20 1.5.2 Design Patterns Discussion 22 1.5.3 Concurrency Patterns 25 1.5.4 Architectural Patterns 26 1.5.5 Further Study on Design Patterns 27 2 Advanced Swing Graphical User Interface Components 29 2.1 Introduction 30 2.2 WebBrowser Using JEditorPane and JToolBar 30 2.2.1 Swing Text Components and HTML Rendering 31 2.2.2 Swing Toolbars 33 2.3 Swing Actions39 2.4 JSplitPane and JTabbedPane 45 VIII Contents 2.5 Multiple-Document Interfaces 52 2.6 Drag and Drop 56 2.7 Internationalization 62 2.8 Accessibility 71 2.9 Internet and World Wide Web Resources 78 3 Model-View-Controller 85 3.1 Introduction 86 3.2 Model-View-Controller Architecture 86 3.3 Observable Class and Observer Interface 88 3.4 JList 107 3.5 JTable 111 3.6 JTree 115 3.6.1 Using DefaultTreeModel 117 3.6.2 Custom TreeModel Implementation 123 4 Graphics Programming with Java 2D and Java 3D 135 4.1 Introduction 136 4.2 Coordinates, Graphics Contexts and Graphics Objects 136 4.3 Java 2D API 138 4.3.1 Java 2D Shapes 140 4.3.2 Java 2D Image Processing 146 4.4 Java 3D API 160 4.4.1 Obtaining and Installing the Java 3D API 161 4.4.2 Java 3D Scenes 161 4.4.3 A Java 3D Example 163 4.5 A Java 3D Case Study: A 3D Game with Custom Behaviors 179 5 Case Study: Java 2D GUI Application with Design Patterns 219 5.1 Introduction 220 5.2 Application Overview 220 5.3 MyShape Class Hierarchy 221 5.4 Deitel DrawingModel 242 5.5 Deitel Drawing Views 254 5.6 Deitel Drawing Controller Logic 260 5.6.1 MyShapeControllers for Processing User Input 260 5.6.2 MyShapeControllers and Factory Method Design Pattern 272 5.6.3 Drag-and-Drop Controller 276 5.7 DrawingInternalFrame Component 287 5.8 ZoomDialog, Action and Icon Components 304 5.9 DeitelDrawing Application 309 6 JavaBeans Component Model 321 6.1 Introduction 322 6.2 Using Beans in Forte for Java Community Edition 323 Contents IX 6.3 Preparing a Class to be a JavaBean 337 6.4 Creating a JavaBean: Java Archive Files 340 6.5 JavaBean Properties 345 6.6 Bound Properties 347 6.7 Indexed Properties and Custom Events 355 6.8 Customizing JavaBeans for Builder Tools 364 6.8.1 PropertyEditors371 6.8.2 Customizers375 6.9 Internet and World Wide Web Resources 379 7 Security 386 7.1 Introduction 387 7.2 Ancient Ciphers to Modern Cryptosystems 388 7.3 Secret-Key Cryptography 389 7.4 Public-Key Cryptography 390 7.5 Cryptanalysis 393 7.6 Key Agreement Protocols 393 7.7 Key Management 394 7.8 Java Cryptography Extension (JCE) 395 7.8.1 Password-Based Encoding with JCE 395 7.8.2 Decorator Design Pattern 405 7.9 Digital Signatures 406 7.10 Public-Key Infrastructure, Certificates and Certification Authorities 407 7.10.1 Java Keystores and keytool 409 7.11 Java Policy Files 410 7.12 Digital Signatures for Java Code 413 7.13 Authentication 417 7.13.1 Kerberos 417 7.13.2 Single Sign-On 417 7.13.3 Java Authentication and Authorization Service (JAAS) 418 7.14 Secure Sockets Layer (SSL) 423 7.14.1 Java Secure Socket Extension (JSSE) 424 7.15 Java Language Security and Secure Coding 429 7.16 Internet and World Wide Web Resources 430 8 Java Database Connectivity (JDBC) 444 8.1 Introduction 445 8.2 Relational-Database Model 446 8.3 Relational Database Overview: The books Database 447 8.4 Structured Query Language (SQL) 452 8.4.1 Basic SELECT Query 453 8.4.2 WHERE Clause 454 8.4.3 ORDER BY Clause 456 8.4.4 Merging Data from Multiple Tables: Joining 459 8.4.5 INSERT INTO Statement 460 8.4.6 UPDATE Statement 461 8.4.7 DELETE FROM Statement 462 X Contents 8.5 Creating Database books in Cloudscape 463 8.6 Manipulating Databases with JDBC 464 8.6.1 Connecting to and Querying a JDBC Data Source 464 8.6.2 Querying the books Database 470 8.7 Case Study: Address-Book Application 479 8.7.1 PreparedStatements480 8.7.2 Transaction Processing 482 8.7.3 Address-Book Application 482 8.8 Stored Procedures 515 8.9 Batch Processing 515 8.10 Processing Multiple ResultSets or Update Counts 517 8.11 Updatable ResultSets518 8.12 JDBC 2.0 Optional Package javax.sql 519 8.12.1 DataSource 519 8.12.2 Connection Pooling 519 8.12.3 RowSets520 8.13 Internet and World Wide Web Resources 520 9 Servlets 530 9.1 Introduction 531 9.2 Servlet Overview and Architecture 533 9.2.1 Interface Servlet and the Servlet Life Cycle 534 9.2.2 HttpServlet Class 536 9.2.3 HttpServletRequest Interface 537 9.2.4 HttpServletResponse Interface 538 9.3 Handling HTTP get Requests 539 9.3.1 Setting Up the Apache Tomcat Server 543 9.3.2 Deploying a Web Application 545 9.4 Handling HTTP get Requests Containing Data 549 9.5 Handling HTTP post Requests 552 9.6 Redirecting Requests to Other Resources 556 9.7 Session Tracking 559 9.7.1 Cookies 560 9.7.2 Session Tracking with HttpSession 569 9.8 Multi-Tier Applications: Using JDBC from a Servlet 577 9.9 HttpUtils Class 584 9.10 Internet and World Wide Web Resources 585 10 JavaServer Pages (JSP) 593 10.1 Introduction 594 10.2 JavaServer Pages Overview 595 10.3 A First JavaServer Page Example 596 10.4 Implicit Objects 598 10.5 Scripting 599 10.5.1 Scripting Components 600 10.5.2 Scripting Example 601 10.6 Standard Actions 604 [...]... snapshot XV 11 61 11 62 11 62 11 63 11 63 11 64 11 65 11 65 11 65 11 91 119 3 11 96 11 97 11 98 11 98 11 99 12 0 3 12 0 4 12 0 4 12 0 9 12 1 4 12 1 4 12 1 7 12 2 0 12 2 3 12 3 2 12 3 2 12 4 2 12 4 4 12 4 8 12 5 2 12 5 3 12 5 8 12 5 9 12 6 0 12 6 0 12 6 2 12 6 4 12 6 5 12 6 6 12 6 9 12 6 9 12 7 3 12 7 6 12 8 1 Contents XVI 23 .11 Updating Entries with Jini Transaction Service 23 .11 .1 Defining the User Interface 23 .11 .2 Discovering the TransactionManager Service 23 .11 .3 Updating... Servlets 20 .3 20 .4 20 .5 10 64 10 67 10 73 10 74 10 74 10 75 10 75 10 77 10 84 10 85 10 85 10 86 10 88 10 89 10 94 10 95 10 95 11 01 110 1 11 07 11 07 11 08 11 10 11 11 111 3 11 17 11 18 11 18 11 19 11 19 1 12 6 1 12 7 11 31 113 4 11 34 11 38 11 38 11 44 11 44 11 45 11 47 11 49 11 49 11 56 Contents 21 Application Servers 21 .1 21 .2 21. 3 21 .4 21 .5 21 .6 Introduction J2EE Specification and Benefits Commercial Application Servers 21 .3 .1 BEA WebLogic 6.0 21 .3 .2. .. JMS 16 .1 16 .2 16 .3 16 .4 16 .5 16 .6 16 .7 16 .8 870 870 873 874 Entity EJBs 15 .1 15 .2 15 .3 867 867 868 868 869 15 .4 15 .5 15 .6 15 .7 906 908 909 919 919 920 9 21 924 926 939 940 940 9 41 945 945 948 949 Illustrations 16 .9 16 .10 16 .11 16 . 12 16 .13 16 .14 16 .15 16 .16 16 .17 XV 16 .19 16 .20 16 . 21 16 .22 16 .23 16 .24 16 .25 16 .26 16 .27 16 .28 16 .29 16 .30 16 . 31 16. 32 16 .33 16 .34 16 .35 16 .36 16 .37 16 .38 16 .39 16 .40 16 . 41 16. 42. .. Transactions 14 .1 14 .2 14 .3 14 .4 XIII 14 .5 14 .6 14 .7 14 .8 14 .9 14 .10 14 .11 14 . 12 14 .13 14 .14 14 .15 848 849 850 8 51 8 52 854 8 62 863 863 864 864 865 865 866 866 Illustrations XIV 14 .16 14 .17 14 .18 14 .19 14 .20 14 . 21 14 .22 14 .23 14 .24 14 .25 14 .26 14 .27 14 .28 14 .29 14 .30 14 . 31 14. 32 14 .33 14 .34 14 .35 XML deployment descriptor for InterestCalculator EJB Specifying JNDI Name for InterestCalculator EJB Deploying... 538 540 54 544 545 546 548 549 5 51 5 52 553 554 555 556 558 559 5 61 565 568 568 569 5 71 577 578 5 82 584 584 596 Illustrations 10 .2 10 .3 10 .4 10 .5 10 .6 10 .7 10 .8 10 .9 10 .10 10 .11 10 . 12 10 .13 10 .14 10 .15 10 .16 10 .17 10 .18 10 .19 10 .20 10 . 21 10 .22 10 .23 10 .24 10 .25 10 .26 10 .27 10 .28 10 .29 10 .30 10 . 31 10. 32 10 .33 10 .34 JSP implicit objects JSP escape sequences Scripting a JavaServer Page—welcome.jsp JSP standard... DrawingFileReaderWriter utility class for saving drawings to files and loading drawings from files Sample XML document generated by DrawingFileReaderWriter 5 .2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 5 .10 5 .11 13 7 13 8 13 9 14 0 14 4 14 7 15 0 15 1 1 52 15 3 15 3 15 4 15 6 1 62 16 3 17 0 17 1 1 72 17 3 17 4 17 5 17 9 18 1 19 8 20 0 20 4 20 5 21 0 22 3 22 3 22 4 23 0 23 2 23 4 23 5 24 0 24 3 24 5 25 2 Illustrations IV 5. 12 5 .13 5 .29 DrawingView class for displaying MyShapes... Component Inspector and Form windows Source Editor window Beans tab of the Component Palette LogoAnimator icon LogoAnimator animation in the Form window 5 .14 5 .15 5 .16 5 .17 5 .18 5 .19 5 .20 5 . 21 5 .22 5 .23 5 .24 5 .25 5 .26 5 .27 5 .28 6.6 6.7 6.8 6.9 6 .10 6 .11 6. 12 6 .13 6 .14 6 .15 25 4 25 8 26 1 26 6 26 7 26 9 27 3 27 6 28 4 28 5 28 5 28 7 304 305 306 307 309 317 324 324 324 325 325 325 326 326 326 327 327 328 328 328 329 Illustrations... 89 89 91 93 94 97 1 02 10 5 10 8 10 8 11 1 1 12 1 12 11 6 11 7 12 3 12 9 Illustrations III 4 Graphics Programming with Java 2D and Java 3D 4 .1 4 .2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4 .10 4 .11 4. 12 4 .13 4 .14 4 .15 4 .16 4 .17 4 .18 4 .19 4 .20 4 . 21 4 .22 4 .23 4 .24 4 .25 4 .26 4 .27 4 .28 Java coordinate system Units are measured in pixels Some Java 2D classes and interfaces The seven state attributes of a Java 2D graphics context... Notifications 24 .3.4 Management Application 24 .3.5 Compiling and Running the Example Internet and World Wide Web Resources 25 Jiro (on CD) 25 .1 25 .2 25.3 25 .4 25 .5 12 8 4 12 8 5 12 8 7 12 8 9 12 9 4 12 9 5 13 01 13 12 13 14 13 19 1 320 1 322 1 322 1 322 13 38 13 42 13 46 13 57 13 60 13 64 Introduction Installation Starting Jiro Dynamic vs Static Services Dynamic Services 25 .5 .1 Dynamic-Service Implementation 25 .6 Static Services 25 .6 .1. .. B.4 .2 EMPTY, Mixed Content and ANY Attribute Declarations Attribute Types B.6 .1 Tokenized Attribute Type (ID, IDREF, ENTITY, NMTOKEN) B.6 .2 Enumerated Attribute Types Conditional Sections Whitespace Characters Internet and World Wide Web Resources B.5 B.6 B.7 B.8 B.9 15 51 1553 15 55 15 62 15 64 15 71 15 71 15 72 15 72 15 77 15 88 15 90 15 94 15 95 15 96 16 02 16 08 16 11 16 12 16 12 16 15 16 16 16 16 16 16 16 18 1 620 1 623 1 627 . Implementation 11 18 20 .2. 1 Customer Remote Interface 11 19 20 .2. 2 CustomerEJB Implementation 11 19 20 .2. 3 CustomerHome Interface 1 126 20 .2. 4 CustomerModel 1 127 20 .3 Address Implementation 11 31 20 .3 .1 Address. Services 12 2 0 22 .7.4 Jini Service Client 12 2 3 22 .8 Introduction to High-Level Helper Utilities 12 3 2 22 .8 .1 Discovery Utilities 12 3 2 22 .8 .2 Entry Utilities 12 4 2 22 .8.3 Lease Utilities 12 4 4 22 .8.4. CMP Entity EJBs 11 49 20 .5 .2 Deploying Deitel Bookstore Servlets 11 56 Contents XV 21 Application Servers 11 61 21 .1 Introduction 11 62 21 .2 J2EE Specification and Benefits 11 62 21. 3 Commercial Application