Tải bản đầy đủ (.pdf) (188 trang)

Advanced Java 2 Platform HOW TO PROGRAM phần 1 pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.85 MB, 188 trang )

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:

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
Contents XI
10.6.1 <jsp:include> Action 605
10.6.2 <jsp:forward> Action 610
10.6.3 <jsp:plugin> Action 613
10.6.4 <jsp:useBean> Action 617
10.7 Directives 634
10.7.1 page Directive 634
10.7.2 include Directive 636
10.8 Custom Tag Libraries 638
10.8.1 Simple Custom Tag 639
10.8.2 Custom Tag with Attributes 643
10.8.3 Evaluating the Body of a Custom Tag 647
10.9 Internet and World Wide Web Resources 653
11 Case Study: Servlet and JSP Bookstore 660
11.1 Introduction 661
11.2 Bookstore Architecture 662
11.3 Entering the Bookstore 664
11.4 Obtaining the Book List from the Database 667
11.5 Viewing a Book’s Details 676
11.6 Adding an Item to the Shopping Cart 683
11.7 Viewing the Shopping Cart 686
11.8 Checking Out 689

11.9 Processing the Order 693
11.10 Deploying the Bookstore Application in J2EE 1.2.1 694
11.10.1 Configuring the books Data Source 695
11.10.2 Launching the Cloudscape Database and J2EE Servers 695
11.10.3 Launching the J2EE Application Deployment Tool 696
11.10.4 Creating the Bookstore Application 697
11.10.5 Creating BookServlet and AddToCartServlet Web Components 698
11.10.6 Adding Non-Servlet Components to the Application 704
11.10.7 Specifying the Web Context, Resource References, JNDI
Names and Welcome Files 706
11.10.8 Deploying and Executing the Application 709
12 Java-Based Wireless Applications
Development and J2ME 716
12.1 Introduction 717
12.2 WelcomeServlet Overview 720
12.3 TipTestServlet Overview 726
12.3.1 Internet Explorer Request 739
12.3.2 WAP Request 746
12.3.3 Pixo i-mode Request 751
12.3.4 J2ME Client Request 755
12.4 Java 2 Micro Edition 757
12.4.1 Connected Limited Device Configuration (CLDC) 758
12.4.2 Mobile Information Device Profile (MIDP) 759
XII Contents
12.4.3 TipTestMIDlet Overview 761
12.5 Installation Instructions 781
12.6 Internet and World Wide Web Resources 785
13 Remote Method Invocation 790
13.1 Introduction 791
13.2 Case Study: Creating a Distributed System with RMI 792

13.3 Defining the Remote Interface 792
13.4 Implementing the Remote Interface 793
13.5 Compiling and Executing the Server and the Client 807
13.6 Case Study: Deitel Messenger with Activatable Server 809
13.6.1 Activatable Deitel Messenger ChatServer 810
13.6.2 Deitel Messenger Client Architecture and Implementation 820
13.6.3 Running the Deitel Messenger Server and Client Applications 836
13.7 Internet and World Wide Web Resources 840
14 Session EJBs and Distributed Transactions 846
14.1 Introduction 847
14.2 EJB Overview 847
14.2.1 Remote Interface 848
14.2.2 Home Interface 848
14.2.3 EJB Implementation 849
14.2.4 EJB Container 849
14.3 Session Beans 849
14.3.1 Stateful Session EJBs 849
14.3.2 Deploying Session EJBs 862
14.3.3 Stateless Session EJBs 869
14.4 EJB Transactions 879
14.4.1 MoneyTransfer EJB Home and Remote Interfaces 879
14.4.2 Bean-Managed Transaction Demarcation 881
14.4.3 Container-Managed Transaction Demarcation 886
14.4.4 MoneyTransfer EJB Client 892
14.4.5 Deploying the MoneyTransfer EJB 898
14.5 Internet and World Wide Web Resources 900
15 Entity EJBs 904
15.1 Introduction 905
15.2 Entity EJB Overview 905
15.3 Employee Entity EJB 906

15.4 Employee EJB Home and Remote Interfaces 906
15.5 Employee EJB with Bean-Managed Persistence 908
15.5.1 Employee EJB Implementation 908
15.5.2 Employee EJB Deployment 918
15.6 Employee EJB with Container-Managed Persistence 920
15.7 Employee EJB Client 925
15.8 Internet and World Wide Web Resources 934
Contents XIII
16 Messaging with JMS 937
16.1 Introduction 938
16.2 Installation and Configuration of J2EE 1.3 939
16.3 Point-To-Point Messaging 940
16.3.1 Voter Application: Overview 940
16.3.2 Voter Application: Sender Side 941
16.3.3 Voter Application: Receiver Side 945
16.3.4 Voter Application: Configuring and Running 951
16.4 Publish/Subscribe Messaging 951
16.4.1 Weather Application: Overview 952
16.4.2 Weather Application: Publisher Side 953
16.4.3 Weather Application: Subscriber Side 958
16.4.4 Weather Application: Configuring and Running 967
16.5 Message-Driven Enterprise JavaBeans 968
16.5.1 Voter Application: Overview 968
16.5.2 Voter Application: Receiver Side 969
16.5.3 Voter Application: Configuring and Running 978
17 Enterprise Java Case Study: Architectural Overview 990
17.1 Introduction 991
17.2 Deitel Bookstore 992
17.3 System Architecture 992
17.4 Enterprise JavaBeans 993

17.4.1 Entity EJBs 993
17.4.2 Stateful Session EJBs 995
17.5 Servlet Controller Logic 995
17.6 XSLT Presentation Logic 995
18 Enterprise Java Case Study: Presentation
and Controller Logic 1009
18.1 Introduction 1010
18.2 XMLServlet Base Class 1011
18.3 Shopping Cart Servlets 1022
18.3.1 AddToCartServlet 1023
18.3.2 ViewCartServlet 1023
18.3.3 RemoveFromCartServlet 1034
18.3.4 UpdateCartServlet 1034
18.3.5 CheckoutServlet 1039
18.4 Product Catalog Servlets 1040
18.4.1 GetAllProductsServlet 1040
18.4.2 GetProductServlet 1046
18.4.3 ProductSearchServlet 1049
18.5 Customer Management Servlets 1053
18.5.1 RegisterServlet 1053
18.5.2 LoginServlet 1057
18.5.3 ViewOrderHistoryServlet 1060
XIV Contents
18.5.4 ViewOrderServlet 1064
18.5.5 GetPasswordHintServlet 1067
19 Enterprise Java Case Study: Business Logic Part 1 1073
19.1 Introduction 1074
19.2 EJB Architecture 1074
19.3 ShoppingCart Implementation 1075
19.3.1 ShoppingCart Remote Interface 1075

19.3.2 ShoppingCartEJB Implementation 1077
19.3.3 ShoppingCartHome Interface 1084
19.4 Product Implementation 1085
19.4.1 Product Remote Interface 1085
19.4.2 ProductEJB Implementation 1086
19.4.3 ProductHome Interface 1088
19.4.4 ProductModel 1089
19.5 Order Implementation 1094
19.5.1 Order Remote Interface 1095
19.5.2 OrderEJB Implementation 1095
19.5.3 OrderHome Interface 1101
19.5.4 OrderModel 1101
19.6 OrderProduct Implementation 1107
19.6.1 OrderProduct Remote Interface 1107
19.6.2 OrderProductEJB Implementation 1108
19.6.3 OrderProductHome Interface 1110
19.6.4 OrderProductPK Primary-Key Class 1111
19.6.5 OrderProductModel 1113
20 Enterprise Java Case Study: Business Logic Part 2 1117
20.1 Introduction 1118
20.2 Customer Implementation 1118
20.2.1 Customer Remote Interface 1119
20.2.2 CustomerEJB Implementation 1119
20.2.3 CustomerHome Interface 1126
20.2.4 CustomerModel 1127
20.3 Address Implementation 1131
20.3.1 Address Remote Interface 1134
20.3.2 AddressEJB Implementation 1134
20.3.3 AddressHome Interface 1138
20.3.4 AddressModel 1138

20.4 SequenceFactory Implementation 1144
20.4.1 SequenceFactory Remote Interface 1144
20.4.2 SequenceFactoryEJB Implementation 1145
20.4.3 SequenceFactoryHome Interface 1147
20.5 Deitel Bookstore Application Deployment with J2EE 1149
20.5.1 Deploying Deitel Bookstore CMP Entity EJBs 1149
20.5.2 Deploying Deitel Bookstore Servlets 1156
Contents XV
21 Application Servers 1161
21.1 Introduction 1162
21.2 J2EE Specification and Benefits 1162
21.3 Commercial Application Servers 1163
21.3.1 BEA WebLogic 6.0 1163
21.3.2 iPlanet Application Server 6.0 1164
21.3.3 IBM WebSphere Advanced Application Server 4.0 1165
21.3.4 JBoss 2.2.2 Application Server 1165
21.4 Deploying the Deitel Bookstore on BEA WebLogic 1165
21.5 Deploying the Deitel Bookstore on IBM WebSphere 1191
21.6 Internet and World Wide Web Resources 1193
22 Jini 1196
22.1 Introduction 1197
22.2 Installing Jini 1198
22.3 Configuring the Jini Runtime Environment 1198
22.4 Starting the Required Services 1199
22.5 Running the Jini LookupBrowser 1203
22.6 Discovery 1204
22.6.1 Unicast Discovery 1204
22.6.2 Multicast Discovery 1209
22.7 Jini Service and Client Implementations 1214
22.7.1 Service Interfaces and Supporting Classes 1214

22.7.2 Service Proxy and Service Implementations 1217
22.7.3 Registering the Service with Lookup Services 1220
22.7.4 Jini Service Client 1223
22.8 Introduction to High-Level Helper Utilities 1232
22.8.1 Discovery Utilities 1232
22.8.2 Entry Utilities 1242
22.8.3 Lease Utilities 1244
22.8.4 JoinManager Utility 1248
22.8.5 Service Discovery Utilities 1252
22.9 Internet and World Wide Web Resources 1253
23 JavaSpaces 1258
23.1 Introduction 1259
23.2 JavaSpaces Service Properties 1260
23.3 JavaSpaces Service 1260
23.4 Discovering the JavaSpaces Service 1262
23.5 JavaSpace Interface 1264
23.6 Defining an Entry 1265
23.7 Write Operation 1266
23.8 Read and Take Operations 1269
23.8.1 Read Operation 1269
23.8.2 Take Operation 1273
23.9 Notify Operation 1276
23.10 Method snapshot 1281
XVI Contents
23.11 Updating Entries with Jini Transaction Service 1284
23.11.1 Defining the User Interface 1285
23.11.2 Discovering the TransactionManager Service 1287
23.11.3 Updating an Entry 1289
23.12 Case Study: Distributed Image Processing 1294
23.12.1 Defining an Image Processor 1295

23.12.2 Partitioning an Image into Smaller Pieces 1301
23.12.3 Compiling and Running the Example 1312
23.13 Internet and World Wide Web Resources 1314
24 Java Management Extensions (JMX) (on CD) 1319
24.1 Introduction 1320
24.2 Installation 1322
24.3 Case Study 1322
24.3.1 Instrument Resources 1322
24.3.2 Implementation of the JMX Management Agent 1338
24.3.3 Broadcasting and Receiving Notifications 1342
24.3.4 Management Application 1346
24.3.5 Compiling and Running the Example 1357
24.4 Internet and World Wide Web Resources 1360
25 Jiro (on CD) 1364
25.1 Introduction 1365
25.2 Installation 1366
25.3 Starting Jiro 1367
25.4 Dynamic vs. Static Services 1369
25.5 Dynamic Services 1369
25.5.1 Dynamic-Service Implementation 1370
25.6 Static Services 1380
25.6.1 Locating Static Services with Class ServiceFinder 1380
25.6.2 Event Service 1381
25.6.3 Log Service 1389
25.6.4 Scheduling Service 1391
25.7 Dynamic Service Deployment 1392
25.7.1 Dynamic–Service Usage 1395
25.8 Management Policies 1409
25.8.1 Policy–Management Deployment 1420
25.9 Closing Notes on the Printer Management Solution 1428

25.10 Internet and World Wide Web Resources 1429
26 Common Object Request Broker Architecture
(CORBA): Part 1 (on CD) 1435
26.1 Introduction 1436
26.2 Step-by-Step 1441
26.3 First Example: SystemClock 1442
26.3.1 SystemClock.idl 1443
26.3.2 SystemClockImpl.java 1444
Contents XVII
26.3.3 SystemClockClient.java 1449
26.3.4 Running the Example 1452
26.4 Technical/Architectural Overview 1453
26.5 CORBA Basics 1458
26.6 Example: AlarmClock 1468
26.6.1 AlarmClock.idl 1468
26.6.2 AlarmClockImpl.java 1469
26.6.3 AlarmClockClient.java 1472
26.7 Distributed Exceptions 1476
26.8 Case Study: Chat 1480
26.8.1 chat.idl 1482
26.8.2 ChatServerImpl.java 1483
26.8.3 DeitelMessenger.java 1488
26.8.4 Running Chat 1493
26.8.5 Issues 1493
26.9 Comments and Comparisons 1498
26.10 Internet and World Wide Web Resources 1499
27 Common Object Request Broker Architecture
(CORBA): Part 2 (on CD) 1508
27.1 Introduction 1509
27.2 Static Invocation Interface (SII), Dynamic Invocation Interface (DII)

and Dynamic Skeleton Interface (DSI) 1510
27.3 BOAs, POAs and TIEs 1514
27.4 CORBAservices 1516
27.4.1 Naming Service 1516
27.4.2 Security Service 1517
27.4.3 Object Transaction Service 1518
27.4.4 Persistent State Service 1519
27.4.5 Event and Notification Services 1520
27.5 EJBs and CORBAcomponents 1523
27.6 CORBA vs. RMI 1529
27.6.1 When to Use RMI 1529
27.6.2 When to Use CORBA 1530
27.6.3 RMI-IIOP 1530
27.7 RMIMessenger Case Study Ported to RMI-IIOP 1531
27.7.1 ChatServer RMI-IIOP Implementation 1532
27.7.2 ChatClient RMI-IIOP Implementation 1538
27.7.3 Compiling and Running the ChatServer and ChatClient 1542
27.8 Future Directions 1543
27.9 Internet and World Wide Web Resources 1543
28 Peer-to-Peer Applications and JXTA 1548
28.1 Introduction 1549
28.2 Client/Server and Peer-to-Peer Applications 1549
28.3 Centralized vs. Decentralized Network Applications 1550
28.4 Peer Discovery and Searching 1551
XVIII Contents
28.5 Case Study: Deitel Instant Messenger 1551
28.6 Defining the Service Interface 1553
28.7 Defining the Service implementation 1555
28.8 Registering the Service 1562
28.9 Find Other Peers 1564

28.10 Compiling and Running the Example 1571
28.11 Improving Deitel Instant Messenger 1571
28.12 Deitel Instant Messenger with Multicast Sockets 1572
28.12.1 Registering the Peer 1572
28.12.2 Finding Other Peers 1577
28.13 Introduction to JXTA 1588
28.14 Internet and World Wide Web Resources 1590
29 Introduction to Web Services and SOAP 1594
29.1 Introduction 1595
29.2 Simple Object Access Protocol (SOAP) 1596
29.3 SOAP Weather Service 1602
29.4 Internet and World Wide Web Resources 1608
A Creating Markup with XML (on CD) 1611
A.1 Introduction 1612
A.2 Introduction to XML Markup 1612
A.3 Parsers and Well-Formed XML Documents 1615
A.4 Characters 1616
A.4.1 Characters vs. Markup 1616
A.4.2 White Space, Entity References and Built-In Entities 1616
A.5 CDATA Sections and Processing Instructions 1618
A.6 XML Namespaces 1620
A.7 Internet and World Wide Web Resources 1623
B Document Type Definition (DTD) (on CD) 1627
B.1 Introduction 1628
B.2 Parsers, Well-Formed and Valid XML Documents 1628
B.3 Document Type Declaration 1629
B.4 Element Type Declarations 1630
B.4.1 Sequences, Pipe Characters and Occurrence Indicators 1631
B.4.2 EMPTY, Mixed Content and ANY 1634
B.5 Attribute Declarations 1636

B.6 Attribute Types 1638
B.6.1 Tokenized Attribute Type (ID, IDREF, ENTITY, NMTOKEN) 1638
B.6.2 Enumerated Attribute Types 1643
B.7 Conditional Sections 1644
B.8 Whitespace Characters 1645
B.9 Internet and World Wide Web Resources 1647
Contents XIX
C Document Object Model (DOM™) (on CD) 1652
C.1 Introduction 1653
C.2 DOM with Java 1654
C.3 Setup Instructions 1657
C.4 DOM Components 1657
C.5 Creating Nodes 1665
C.6 Traversing the DOM 1668
C.7 Internet and World Wide Web Resources 1671
D XSL: Extensible Stylesheet Language
Transformations (XSLT) (on CD) 1676
D.1 Introduction 1677
D.2 Applying XSLTs with Java 1677
D.3 Templates 1679
D.4 Creating Elements and Attributes 1680
D.5 Iteration and Sorting 1683
D.6 Conditional Processing 1687
D.7 Combining Style Sheets 1690
D.8 Variables 1695
D.9 Internet and World Wide Web Resources 1695
E Downloading and Installing J2EE 1.2.1 (on CD) 1699
E.1 Introduction 1699
E.2 Installation 1699
E.3 Configuration 1700

E.3.1 JDBC Drivers and Data Sources 1700
E.3.2 HTTP properties 1700
F Java Community ProcessSM (JCP) (on CD) 1701
F.1 Introduction 1701
F.2 Participants 1701
F.2.1 Program Management Office 1701
F.2.2 Executive Committee 1701
F.2.3 Experts 1702
F.2.4 Members 1702
F.2.5 Public Participation 1702
F.3 Java Community Process 1702
F.3.1 Initiation Phase 1702
F.3.2 Community Draft Phase 1703
F.3.3 Public Draft Phase 1704
F.3.4 Final Phase 1704
F.3.5 Maintenance Phase 1704
G Java Native Interface (JNI) (on CD) 1705
G.1 Introduction 1706
XX Contents
G.2 Getting Started with Java Native Interface 1706
G.3 Accessing Java Methods and Objects from Native Code 1710
G.4 JNI and Arrays 1718
G.5 Handling Exceptions with JNI 1722
G.6 Internet and World Wide Web Resources 1733
H Career Opportunities (on CD) 1738
H.1 Introduction 1739
H.2 Resources for the Job Seeker 1740
H.3 Online Opportunities for Employers 1741
H.3.1 Posting Jobs Online 1743
H.3.2 Problems with Recruiting on the Web 1745

H.3.3 Diversity in the Workplace 1745
H.4 Recruiting Services 1746
H.4.1 Testing Potential Employees Online 1747
H.5 Career Sites 1748
H.5.1 Comprehensive Career Sites 1748
H.5.2 Technical Positions 1749
H.5.3 Wireless Positions 1750
H.5.4 Contracting Online 1750
H.5.5 Executive Positions 1751
H.5.6 Students and Young Professionals 1752
H.5.7 Other Online Career Services 1753
H.6 Internet and World Wide Web Resources 1754
I Unicode® (on CD) 1762
I.1 Introduction 1763
I.2 Unicode Transformation Formats 1764
I.3 Characters and Glyphs 1765
I.4 Advantages/Disadvantages of Unicode 1766
I.5 Unicode Consortium’s Web Site 1766
I.6 Using Unicode 1767
I.7 Character Ranges 1770
Index 1774
Illustrations
1 Introduction
1.1 Gang-of-four 23 design patterns. 21
1.2 Gang-of-four design patterns used in Advanced Java 2 Platform
How to Program.22
2 Advanced Swing Graphical User Interface Components
2.1 WebBrowserPane subclass of JEditorPane for viewing Web
sites and maintaining URL history. 31
2.2 Toolbars for navigating the Web in Internet Explorer and Mozilla. 34

2.3 WebToolBar JToolBar subclass for navigating URLs in a
WebBrowserPane.35
2.4 WebBrowser application for browsing Web sites using
WebBrowserPane and WebToolBar.38
2.5 ActionSample application demonstrating the Command design
pattern with Swing Actions. 40
2.6 Action class static keys for Action properties. 45
2.7 FavoritesWebBrowser application for displaying two Web pages
side-by-side using JSplitPane.45
2.8 Tabbed interface of Display Properties dialog box in Windows 2000. 48
2.9 TabbedPaneWebBrowser application using JTabbedPane to browse
multiple Web sites concurrently. 49
2.10 MDIWebBrowser application using JDesktopPane and
JInternalFrames to browse multiple Web sites concurrently. 52
2.11 DnDWebBrowser application for browsing Web sites that also
accepts drag-and-drop operations for viewing HTML pages. 57
2.12 WebToolBar that uses ResourceBundles for internationalization. 63
2.13 MyAbstractAction AbstractAction subclass that provides
set methods for Action properties. 66
II Illustrations
2.14 WebBrowser that uses ResourceBundles for internationalization. 67
2.15 BrowserLauncher application for selecting a Locale and
launching an internationalized WebBrowser.68
2.16 Properties file for default Locale (US English)—
StringsAndLabels.properties.70
2.17 Properties file for French Locale—
StringsAndLabels_fr_FR.properties.71
2.18 ActionSample2 demonstrates Accessibility package. 72
2.19 Actions sampleAction and exitAction of ActionSample2.77
2.20 AccessibleDescription of sampleButton.77

2.21 AccessibleDescription of exitButton.78
2.22 Sample Action menu item description. 78
2.23 Exit menu item description. 78
3 Model-View-Controller
3.1 Model-view-controller architecture. 87
3.2 Delegate-model architecture in Java Swing components. 87
3.3 AccountManager application MVC architecture. 89
3.4 Account Observable class that represents a bank account. 89
3.5 AbstractAccountView abstract base class for observing Accounts. 91
3.6 AccountTextView for displaying observed Account information
in a JTextField.93
3.7 AccountBarGraphView for rendering observed Account
information as a bar graph. 94
3.8 AssetPieChartView for rendering multiple observed asset
Accounts as a pie chart. 97
3.9 AccountController for obtaining user input to modify Account
information. 102
3.10 AccountManager application for displaying and modifying Account
information using the model-view-controller architecture. 105
3.11 JList and ListModel delegate-model architecture. 108
3.12 PhilosophersJList application demonstrating JList and
DefaultListModel.108
3.13 TableModel interface methods and descriptions. 111
3.14 JTable and TableModel delegate-model architecture. 112
3.15 PhilosophersJTable application demonstrating JTable and
DefaultTableModel.112
3.16 JTree showing a hierarchy of philosophers. 116
3.17 PhilosophersJTree application demonstrating JTree and
DefaultTreeModel.117
3.18 FileSystemModel implementation of interface TreeModel

to represent a file system. 123
3.19 FileTreeFrame application for browsing and editing a file system
using JTree and FileSystemModel.129
Illustrations III
4 Graphics Programming with Java 2D and Java 3D
4.1 Java coordinate system. Units are measured in pixels. 137
4.2 Some Java 2D classes and interfaces. 138
4.3 The seven state attributes of a Java 2D graphics context. 139
4.4 Demonstrating some Java 2D shapes. 140
4.5 Demonstrating Java 2D paths. 144
4.6 Class ImagePanel allows for displaying and filtering BufferedImages. 147
4.7 Java2DImageFilter interface for creating Java 2D image filters. 150
4.8 Classes that implement BufferedImageOp and RasterOp.151
4.9 InvertFilter inverts colors in a BufferedImage.152
4.10 SharpenFilter sharpens edges in a BufferedImage.153
4.11 BlurFilter blurs the colors in a BufferedImage.153
4.12 ColorFilter changes the colors in a BufferedImage.154
4.13 Java 2D image-processing application GUI. 156
4.14 Java 3D Group, Leaf and NodeComponent subclasses. 162
4.15 Creating a Java 3D SimpleUniverse with content. 163
4.16 Demonstrating MouseRotate behavior. 170
4.17 Demonstrating MouseTranslate behavior. 171
4.18 Demonstrating MouseZoom behavior. 172
4.19 Demonstrating changing color in Java 3D. 173
4.20 Demonstrating texture mapping in Java 3D. 174
4.21 ControlPanel provides Swing controls for Java3DWorld.175
4.22 GUI for Java3DWorld and ControlPanel.179
4.23 Class Java3DWorld1 creates the 3D-game environment. 181
4.24 Implementing collision detection in a Java 3D application. 198
4.25 Behavior that enables the user to navigate a 3D shape. 200

4.26 Keys for navigating the 3D scene in Navigator.204
4.27 Implementing a position-checking Behavior.205
4.28 Implementing Swing controls for the Java3DWorld1.210
5 Case Study: Java 2D GUI Application with Design Patterns
5.1 Deitel Drawing application showing randomly drawn shapes
(Exercise 5.8) and a ZoomDrawingView (Fig. 5.13). 223
5.2 Large-scale view of drawing from Fig. 5.1. 223
5.3 MyShape abstract base class for drawing objects. 224
5.4 MyLine subclass of class MyShape that represents a line. 230
5.5 MyRectangle subclass of class MyShape that represents a rectangle. 232
5.6 MyOval subclass of class MyShape that represents an oval. 234
5.7 MyText subclass of class MyShape that represents a string of text. 235
5.8 MyImage subclass of class MyShape that represents a JPEG
image in a drawing. 240
5.9 DrawingModel Observable class that represents a drawing
containing multiple MyShapes. 243
5.10 DrawingFileReaderWriter utility class for saving drawings
to files and loading drawings from files. 245
5.11 Sample XML document generated by DrawingFileReaderWriter.252
IV Illustrations
5.12 DrawingView class for displaying MyShapes in a DrawingModel.254
5.13 ZoomDrawingView subclass of DrawingView for displaying
scaled MyShapes. 258
5.14 MyShapeController abstract base class for controllers that handle
mouse input. 261
5.15 BoundedShapeController MyShapeController subclass for
controlling MyLines, MyOvals and MyRectangles. 266
5.16 MyLineController MyShapeController subclass for
drawing MyLines. 267
5.17 MyTextController MyShapeController subclass for

adding MyText instances to a drawing. 269
5.18 MyShapeControllerFactory class for creating appropriate
MyShapeController for given MyShape type. 273
5.19 DragAndDropController for moving MyShapes between
drawings and adding JPEG images to drawings using drag and drop. 276
5.20 DragSourceListener interface methods and their descriptions. 284
5.21 DropTargetListener interface methods and their descriptions. 285
5.22 TransferableShape enables DragAndDropController
to transfer MyShape objects through drag-and-drop operations. 285
5.23 DrawingInternalFrame class that provides a user interface
for creating drawings. 287
5.24 DrawingFileFilter is a FileFilter subclass that enables
users to select Deitel Drawing files from JFileChooser dialogs. 304
5.25 ZoomDialog for displaying DrawingModels in a scalable view. 305
5.26 AbstractDrawingAction abstract base class for Actions. 306
5.27 GradientIcon implementation of interface Icon that draws a gradient. 307
5.28 DeitelDrawing application that uses a multiple-document interface for
displaying and modifying DeitelDrawing drawings. 309
5.29 SplashScreen class for displaying a logo while the application loads. 317
6 JavaBeans Component Model
6.1 Forte for Java Community Edition 2.0. 324
6.2 Install New JavaBean menu item. 324
6.3 Install JavaBean dialog. 324
6.4 Select JavaBean and Palette Category dialogs. 325
6.5 Beans tab in the Component Palette and tooltip for
LogoAnimator JavaBean. 325
6.6 Filesystems tab in the Explorer window. 325
6.7 Development directory selected in Explorer window. 326
6.8 New menu item. 326
6.9 New Template Chooser dialog. 326

6.10 GUI Editing tab of Forte. 327
6.11 Component Inspector and Form windows. 327
6.12 Source Editor window. 328
6.13 Beans tab of the Component Palette.328
6.14 LogoAnimator icon. 328
6.15 LogoAnimator animation in the Form window. 329
Illustrations V
6.16 Component Inspector with LogoAnimator Properties sheet. 329
6.17 Component Inspector drop down-menu for the background property. 330
6.18 Changing background color of LogoAnimator.330
6.19 AnimationWindow selected in Explorer.331
6.20 Selecting FlowLayout in the Explorer menu. 331
6.21 Swing tab of the Component Palette.332
6.22 JButton icon in the Component Palette.332
6.23 Adding a JButton to AnimationWindow.332
6.24 Editing text property of JButton.332
6.25 Component Palette Selection mode.333
6.26 Component Palette Connection mode.333
6.27 Select Connection mode.333
6.28 Connecting JButton and LogoAnimator.334
6.29 Connection Wizard dialog. 334
6.30 Select actionPerformed event. 335
6.31 Selecting method startAnimation for the target component. 335
6.32 Select Execute from Explorer menu. 336
6.33 AnimationWindow running in Forte.336
6.34 Definition of class LogoAnimator.337
6.35 Compile option in the Source Editor menu. 341
6.36 Method file manifest.tmp for the LogoAnimator bean. 341
6.37 Add images directory to LogoAnimator.jar.344
6.38 Manifest tab of JAR Packager dialog. 344

6.39 LogoAnimator2 with property animationDelay.345
6.40 LogoAnimator2 bean with property animationDelay exposed
in Forte’s Component Inspector.347
6.41 Definition for class SliderFieldPanel.348
6.42 Manifest file for the SliderFieldPanel JavaBean. 353
6.43 Change properties currentValue and maximumValue.353
6.44 Select propertyChange event. 354
6.45 Select animationDelay property of LogoAnimator2.354
6.46 Select currentValue Bound Property. 354
6.47 JFrame with LogoAnimator2 and SliderFieldPanel.355
6.48 ColorEvent custom-event class indicating a color change. 356
6.49 ColorListener interface for receiving colorChanged notifications. 356
6.50 Definition of class ColorSliderPanel. 357
6.51 Manifest file for the ColorSliderPanel JavaBean. 362
6.52 Selecting colorChanged method in Connection Wizard.362
6.53 Selecting setBackground method for target LogoAnimator2.363
6.54 Entering user code in Connection Wizard.363
6.55 Using the ColorSliderPanel to change the background color of
LogoAnimator2.363
6.56 SliderFieldPanelBeanInfo exposes properties and events for
SliderFieldPanel.364
6.57 Properties and events exposed by SliderFieldPanelBeanInfo.368
6.58 MaximumValueEditor is a PropertyEditor for
SliderFieldPanel’s maximumValue property. 372

×