Java Expert Solutions
JAVA
Expert Solutions
by Mark Wutka, et. al.
C O N T E N T S
Introduction
Chapter 1 What Is Java?
- Java as a Web Programming Language
- Java as an Applications Programming Language
- New Features on the Horizon
- Java as an Embedded Systems Language
Chapter 2 Embedding Applets in Web Pages
Chapter 3 Applet Security Restrictions
- Applet Security
- File Access Restrictions
- Network Restrictions
- Other Security Restrictions
- Getting Around Security Restrictions
Chapter 4 Displaying Images
- Images in Java
- Displaying Simple Images
- Creating Your Own Images
- Displaying Other Image Formats
- Manipulating Images
- Filtering Image Colors
- Downloading Images
Chapter 5 Animating Images
- Animation
- An Animation Driver
- Animating Image Sequences
- Animating Portions of an Image
- Animating with a Filter
- Cycling the Color Palette
- Animating Graphics
- Eliminating Flicker
Chapter 6 Communicating with a Web Server
- Java and Web Servers
- Getting Files Using the URL Class
- Getting Files Using Sockets
- Performing a Query with GET
- Posting Data with the URL Class
- Posting Data Using Sockets
- Supporting the Cookie Protocol
Chapter 7 Creating Smarter Forms
- Smarter Forms
- Creating Forms with the AWT
- Checking for Errors on the Client Side
- Adding Context-Sensitive Help
- Creating Dynamic Forms
- Loading Another URL from an Applet
- Creating Image Maps with Hot Spots
Chapter 8 Reading and Writing Files from an Applet
- Applets and Files
- Using the JFS Filesystem for Applets
- Saving Files Using HTTP Post
- Storing and Retrieving Files with FTP
Chapter 9 Creating Reusable Graphics Components
- Reusable Components
- The Command Pattern
- Creating a Reusable Image Button
- Using the Observer Interface
- Using Observables for Other Classes
Chapter 10 Inter-Applet Communication
- Locating Other Applets
- Exchanging Data Using Piped Streams
- Creating Multi-Client Pipes
- Sharing Information with Singleton Objects
Chapter 11 Sending E-Mail from an Applet
Chapter 12 Protecting Applet Code
- Protecting Your Code from Unauthorized Use
- Embedding Copyrights in Your Code
- Verifying the Origin of the Applet
- Hiding Information in Your Applet
- Obfuscating a Working Program
Chapter 13 Running Applets as Applications
- Differences Between Applets and Applications
- Allowing an Applet to Run as an Application
- The Applet's Runtime Environment
- Creating an Applet Context
Chapter 14 Creating Your Own Class Archive Files
- Class Archive Files
- Creating Your Own Archive File with Info-ZIP
- Viewing the Contents of a Zip Archive
- Adding Classes Directly to the Browser's Library
- Creating Class Archives with Other Zip Archivers
- Creating Cabinet Files for Internet Explorer
Chapter 15 Accessing Databases with JDBC
- Organizing Your Data for a Relational Database
- Designing Client/Server Database Applications
- The Connection Class
- Handling SQL Statements
- Retrieving Results in JDBC
- Handling Exceptions in JDBC-SQLException Class
- Handling Exceptions in JDBC-SQLWarnings Class
- Handling Date and Time
- Handling SQL Types
- JDBC in Perspective
Chapter 16 Creating 3-Tier Distributed Applications with RMI
- Creating 3-Tier Applications
- RMI Features
- Creating an RMI Server
- Creating an RMI Client
- Creating Peer-to-Peer RMI Applications
- Garbage Collection, Remote Objects, and Peer-to-Peer
Chapter 17 Creating CORBA Clients
- Defining IDL Interfaces
- Compiling IDL Interfaces for Java Clients
- Writing a Client Applet
- Handling Exceptions
- CGI Programs, Java.net.*, and Java.io.* May Not Be the Best Choices
- Using the Dynamic Invocation Interface and the Interface Repository
- Using Filters
- Some Points About Distributed System Architecture
Chapter 18 Using CORBA IDL with Java
- What Is CORBA?
- Sun's IDL to Java Mapping
- Structures
- Using CORBA in Applets
- Creating CORBA Clients with JavaIDL
- Creating CORBA Clients with VisiBroker
Chapter 19 Creating CORBA Servers
Chapter 20 Increasing Graphics Performance
Chapter 21 Download Strategies
- Huffman Coding and Lempel-Ziv Compression
- Delayed Downloading
- Providing Local Libraries
- Downloading Classes in Zipped Format
- Packaging Classes in Jars and Cabinets
Chapter 22 Faster Image Downloads
Chapter 23 Creating Web Services in Java
- Using Java Objects Instead of CGI
- The Servlet API
- The Web Server as a Computing Server
- Adding Web Access to Your Java Applications
- Migrating off the Web Server in the Future
Chapter 24 Writing Web Services for Jeeves
- What Is Jeeves?
- The Jeeves HTTP Server
- Extending Jeeves' Functionality with Servlets
- Building a Database Servlet
- Building a Simple Autonomous Agent System with Jeeves
Chapter 25 Writing Web Services for Jigsaw
- Architectural Overview
- Jigsaw Interface
- Installation and Setup of the Jigsaw HTTP Server
- Adding Content to the Jigsaw Server
- Extending the Server with Java
- Writing Resource Filters in Java
- Handling Forms and the POST Method in Java
Chapter 26 Securing Applets with Digital Signatures
- What Are Digital Signatures?
- Allowing More Access for Signed Applets
- Using a Third Party for Applet Signatures
- Potential Security Problems with Digital Signatures
- Obtaining a Digital Signature Certificate
- Other Uses for Digital Signatures
Chapter 27 Encrypting Data
- Choosing the Right Kind of Encryption
- Guarding Against Malicious Attacks
- Getting Encryption Software
Chapter 28 Accessing Remote Systems Securely
- Getting a Secure Web Server
- Preventing Impersonations
- Accessing Remote Data
- Passing Keys to Clients
- Implementing a Single-Client Secure Server
- Implementing a Multiclient Secure Server
- Creating Other Secure Remote Access Programs
Chapter 29 Creating a Java Shopping Cart
Chapter 30 Performing Secure Transactions
- Letting Customers Digitally Sign Orders
- Using Encryption in All Network Communications
- Creating Java Services for Netscape Servers
- Making Server-Side Applets Work on Different Web Servers
- Performing Secure Transactions
Chapter 31 Java Electronic Commerce Framework (JECF)
- The Difficulties of Electronic Commerce
- Creating Online Services with the JECF
- Storing Information in the Wallet Database
- Implementing a Shopping Cart Applet with the JECF
- Offering Services with Cassettes
- JECF Availability
- Getting More Information About the JECF
Chapter 32 Encapsulating Legacy Systems
- Focusing on Function, not Form
- Providing Access to New Systems
- Using CORBA to Open Up a Closed System
- Encapsulating a TCP/IP System
- Encapsulating with Native Method Calls
- Encapsulating by Emulating a User
- Getting Assistance from the Legacy System
- Presenting a Different Interface
- Combining Multiple Systems
- Some Real-World Examples
Chapter 33 Web-Enabling Legacy Systems
Chapter 34 Interfacing with CICS Systems
- A Thumbnail Sketch of CICS
- The CICS External Call Interface
- The Java-CICS Gateway API
- Creating Multiple-Call LUWs
- Creating Web Interfaces to CICS
- Providing a CORBA Interface to CICS
Chapter 35 Adding Additional Protocols to HotJava
- Writing a Protocol Handler
- Using Protocol Handlers with HotJava
- Using Protocol Handlers with Your Own Applications
- More on URLStreamHandlerFactory
Chapter 36 Adding New MIME Types to HotJava
- Writing Content Handlers
- Using Content Handlers with HotJava
- Using Content Handlers with Your Own Applications
Chapter 37 Creating Multi-User Programs in Java
- Designing Multi-User Applications
- Adding Socket-Based Access to Multi-User Applications
- Other Issues When Dealing with Sockets
- Adding RMI Access to Multi-User Applications
Chapter 38 Creating On-Demand Multimedia Services
- Java's Suitability for On-Demand Applications
- Using the On-Demand Audio Applet
- Adding Sound to Applets
- On-Demand Music Applet Code Review
- Java Shortcomings
- New Features
Chapter 39 Implementing a Multimedia Encyclopedia
- Java's Suitability for Multimedia Applications
- Using the Multimedia Encyclopedia
- Adding Images and Sound to Applets
- The On-Line Multimedia Encyclopedia In-Depth
- Shortcomings
- New Features
Chapter 40 Implementing Java Interfaces for Non-Traditional Devices
- Characteristics of Non-Traditional Devices
- The New Computing Model
- Designing Applications to Support Non-Traditional Devices
- Designing User Interfaces for Small Devices
- Creating Reusable Components for Small Devices
Credits
Java Expert Solutions
Copyright 1997 by Que © Corporation.
All rights reserved. Printed in the United States of America. No part of this book may be used or reproduced in any form or by any means, or stored in a database or retrieval system, without prior written permission of the publisher except in the case of brief quotations embodied in critical articles and reviews. Making copies of any part of this book for any purpose other than your own personal use is a violation of United States copyright laws. For information, address Que Corporation, 201 W. 103rd Street, Indianapolis, IN 46290. You may reach Que's direct sales line by calling 1-800-428-5331.
ISBN: 0-7897-0935-x
HTML conversion by :
M/s. LeafWriters (India) Pvt. Ltd.
Website : http://leaf.stpn.soft.net
e-mail : leafwriters@leaf.stpn.soft.net
| President | Roland Elgey | Publisher | Joseph B. Wikert |
| Publishing Manager | Jim Minatel | Title Manager | Steven M. Schafer |
| Editorial Services Director | Elizabeth Keaffaber | Managing Editor | Sandy Doell |
| Director of Marketing | Lynn E. Zingraf | Acquisitions Manager | Cheryl D. Willoughby |
| Acquisitions Editor | Stephanie Gould | Product Directors | Mark Cierzniak, Jon Steever |
| Production Editor | Sean Dixon | Editors | Kelly Brooks, Judith Goode, Sidney Jones, Kelly Oliver |
| Product Marketing | Kim Margolius | Assistant Product Marketing Manager | Christy M. Miller |
| Strategic Marketing Manager | Barry Pruett | Technical Editors | Jim Hoffman, Russ Jacobs, Ernie Sanders, Eugene W. Sotirescu, Steve Tallon |
| Technical Support Specialist | Nadeem Muhammed | Acquisitions Coordinator | Jane K. Brownlow |
| Software Relations Coordinator | Patty Brooks | Editorial Assistant | Andrea Duvall |
| Interior Book Designer | Barbara Kordesh | Cover Designer | Barbara Kordesh |
| Production Team | Kevin Cliburn, Tammy Graham, Jason Hand, Heather Howell, Dan Julian, Bob LaRoche, Casey Price, Erich Richter, Laura Robbins, Marvin Van Tiem, Paul Wilson | ||
| Indexer | Chris Barrick | ||
Acknowledgments
Writing a book like this is quite an experience, and one of the most important parts of that experience has been the people I have worked with and the people who helped me get through it.
I would especially like to thank my wife Ceal, who somehow managed to keep me close to my normal level of sanity (which is minimal at best). Thanks also go to Chris, Amy, Samantha, and Kaitlynn, who had to endure endless hours of clicking keys and to my Mom, who taught me, by example, how to work hard and to strive constantly to improve myself.
Joe Weber, author of Special Edition Using Java, provided some excellent suggestions about the outline for this book, as well as some good advice about being an author. In addition, Cliff McCartney provided me with technical feedback on various aspects of the book, especially in the area of legacy system migration-a subject near and dear to both of our hearts.
This book was not written by a single person. I am extremely grateful for the work of the other authors. The technical expertise that each of them brought to this book has truly made it a book of expert solutions.
I would also like to thank the staff at Que, who have been great to work with-Stephanie Gould, Mark Cierzniak, Ben Milstead, Jon Steever, Sean Dixon, and the many people behind the scenes.
Finally, I would like to thank Geddy, Alex, and Neil for EXCELLENT music to code by. You guys have gotten me through hundreds of thousands of lines of code.
About the Authors
David W. Baker is a systems developer for BBN Planet, a business unit of BBN Corporation. He specializes in software development and system integration for Internet solutions. He also works as a freelance game writer, authoring materials for various roleplaying games. Until recently, he worked for Second Nature Interactive, a software development company, where he served as a Senior Game Writer. David's home page is available at http://www.netspace.org/users/dwb/.
David P. Boswell lives in Brigham City, Utah, with his wife Carma and four children. He works as a programmer/analyst for Thiokol Corporation and as an independent Internet consultant. David can be reached via http://www.daves.net or dave@daves.net.
Ken Cartwright is a software engineer with Science Applications International Corp. He has received a bachelor's degree from the University of Oklahoma and a master's degree in information systems and software engineering from George Mason University. He has been developing complex software systems for several years and has spent the last three years concentrating on object-oriented distributed system development using CORBA and object-oriented databases. Some of his most recent projects utilized the combination of Java and CORBA to support client/server systems. Ken can be reached at KenCartwright@msn.com.
David Edgar Liebke (liebke@scripps.edu) works at The Scripps Research Institute in La Jolla, CA. A member of the Research Computing group, he develops Java-based information systems and deals with issues of network security. David graduated from UC San Diego with a B.S. in cognitive science, where he studied artificial intelligence, neural networking, and emergent computation. He currently lives in Irvine, CA with his spousal-type unit, Rochelle, who is completing her doctorate in cognitive science. David hopes to one day rid the world of tyranny, or at least proprietary software "standards."
Tom Lockwood has 12 years' experience as a technical writer and marketing specialist with several computer graphic companies. He is currently employed at Cinebase Software where he championed the development of its Web site. Tom is also a freelance writer, a softball coach, and, most proudly, an Aries. He can be reached at tom.lockwood@cinesoft.com or via his personal Web site at http://www.cris.com/~tlockwoo.
Stephen N. Matsuba is cofounder of Alt.Reality Technologies Corporation, a company developing virtual reality and multimedia applications, and SHOC Interactive, a company developing multimedia games and educational applications. In his other life, he is completing his Ph.D. in computational linguistics and English literature at York University, Canada. His research interests include Shakespeare, literary theory, computational linguistics, artificial intelligence and cognitive science, computer applications in humanities research and education, VR, and multimedia design. He also coauthored Special Edition Using VRML (Que, 1996) with Bernie Roehl.
George Menyhert is currently the Director of the Harmony Product and a member of the technical staff at Cinebase Software where he concentrates on multimedia application engineering. He is also a freelance Java developer. George has a degree in engineering from the University of Cincinnati. He can be reached via his Web page at http://w3.one.net/~menyhert or through one of his various e-mail accounts: george.menyhert@cinesoft.com, menyhert@one.net, or menyhert@acm.org.
Krishna Sankar has been a computer professional since 1980. He has worked on strategic business systems for companies like HP, AT&T, Pratt & Whitney, Testek, Ford, TRW, Caterpillar, Qantas Airlines, and Air Canada, as well as for the U.S. Air Force and U.S. Navy. He still believes in information re-engineering and development of competitive business systems and is excited about the possibilities of intranet applets and servlets in those areas. He has two master's degrees, one in production engineering and the other in computer science. He is now pursuing his MBA. He is a Microsoft Product Specialist as well as a Lotus Certified Professional. He is the founder of U.S. Systems & Services, a Silicon Valley intranet systems and Java technology company. Nowadays, you can meet him in the corridors of venture capitalists and banks promoting products "for those whose life is not Internet but want to leverage the net to enjoy it."
Mark Wutka is a senior systems architect who refuses to give up his programming hat. For the past two years he has worked as the chief architect on a large, object-oriented distributed system providing automation for the flight operations division of a major airline. Over the past eight years, he has designed and implemented numerous systems in C, C++, Smalltalk, and Java for that same airline.
He is currently the Vice President of Research and Development for Pioneer Technologies, a consulting firm specializing in distributed systems and legacy system migration. He can be reached via e-mail at wutka@netcom.com. He also claims responsibility for the random bits of humor found at http://www.webcom.com/wutka.
We'd Like to Hear from You!
As part of our continuing effort to produce books of the highest possible quality, Que would like to hear your comments. To stay competitive, we really want you, as a computer book reader and user, to let us know what you like or dislike most about this book or other Que products.
You can mail comments, ideas, or suggestions for improving future editions to the address below, or send us a fax at (317) 581-4663. Our staff and authors are available for questions and comments through our Internet site, at http://www.mcp.com, and Macmillan Computer Publishing also has a forum on CompuServe (type GO QUEBOOKS at any prompt).
In addition to exploring our forum, please feel free to contact me personally to discuss your opinions of this book: I'm mcierzniak@que.mcp.com on the Internet.
Thanks in advance-your comments will help us to continue publishing the best books available on new computer technologies in today's market.
Mark Cierzniak
Product Development Specialist
Que Corporation
201 W. 103rd Street
Indianapolis, Indiana 46290
USA
Introduction
by Mark Wutka
Java is one of the most significant software products to hit the scene in a long time. Unlike Netscape, whose impact was big and immediate, Java's full impact won't be realized for a long time. Java is more than just a programming language. It requires a different mindset when developing applications.
Sure, you can use Java to spruce up your Web pages-it works quite well for that. This book will even give you some tips on ways to do it. But that's not the main purpose of Java. If you only use it for pretty Web pages, you are missing a lot.
Hacking Java: The Java Professional's Resource Kit not only gives you lots of useful Java classes and programming tips, it relates the "vision" of Java. You get an overview from the 30,000-foot level, as well as from down in the trenches, to borrow some management clichés. Both of these views are important. When you're digging a trench, you still need to look up to see where you're headed. Java will have a significant impact on the future of software development, and even the future of technology. If you don't already understand why this is so, you need this book.
One of the important things to realize about Java is that it is young and still evolving. There are many features yet to come, and many more uses of Java to be discovered. This book will help guide you in making design decisions that may be affected by some of these new applications of Java.
Who Should Read This Book
This book addresses Java on several different levels. Some people will be interested in hard-core programming techniques. There are plenty of those here. You may know the language, but you want to use it to solve different problems that aren't addressed in any of the Java programming books.
You may be a software designer, looking for new design techniques. This book has plenty of good object-oriented design strategies that apply not only to Java but to other O-O languages as well.
If you're a system architect creating your company's information infrastructure, there's plenty in here for you, too. This book discusses many architectural issues and shows you situations where you can use Java that you've probably never thought of. This is especially true when it comes to the overall philosophy of Java and its multitude of uses.
This book is not an introduction to programming in Java. There is no discussion of what classes and methods are. Special Edition Using Java by Que will give you a good introduction to Java. This book is meant to complement Special Edition Using Java, giving you the kind of advice that you don't get from a book on programming.
What This Book Is About
Hacking Java: The Java Professional's Resource Kit is more than just a how-to book. It's also a what-to book. A how-to book assumes that you already know exactly what you want to do, and it gives you step-by-step instructions showing you how to do it. This book gives you ideas about what to do with Java, and then tells you how to implement them.
How This Book Is Organized
This book starts out by addressing some of the burning issues of creating applets. It provides suggestions for improving the performance of your applets, as well as ways to get around some of the restrictions imposed on applets.
Section II discusses some of the aspects of Java applications, including a way to run an applet as an application. This section also discusses the JDBC database interface and the remote method invocation facility.
Section III discusses some of the CORBA products available for Java, how to use them, and what you can do with them. If you are unfamiliar with CORBA, you also get a brief introduction to CORBA.
Section IV shows you how you can speed up your applets, both in the download phase and once the applet is running.
Section V introduces some of the Java Web servers that are now available. You can use Java to implement new Web services that you previously could only do with CGI. In addition, since the servers are written in Java, you can run them anywhere you can run Java.
Section VI delves into some of the deeper aspects of security. It introduces digital signatures and data encryption, and discusses some of the issues involved in protecting your communications.
Section VII shows you how you can use Java to do business over the Web. It discusses some of the aspects of electronic commerce and shows you how to perform secure transactions.
Section VIII deals with "legacy" systems and how you can use Java to connect these older systems to the Internet. There is a large amount of system design philosophy in this section, much of which is applicable no matter what language you are using.
Section IX shows you how to expand capabilities of the HotJava browser, which is written entirely in Java. You will learn how you can add new networking protocols and how you can make HotJava understand new data formats.
Section X introduces some of the multimedia capabilities of Java. This is one field where Java will be expanding greatly. This section suggests possible uses of Java in the multimedia realm.
Section XI discusses some of the issues involved with running Java on small devices like cellular phones and personal digital assistants (PDA). As these devices become more readily available, your systems will have to cooperate with them. This section gives you guidelines that let you start planning for these devices now.
On the CD, three chapters will show you how Java integrates with the Virtual Reality Markup Language (VRML). You will see how you can add whole new dimensions to your Java programs, literally.
How to Use This Book
You can use this book either as a cookbook or as a learning tool. If you have a specific problem that you need to solve, you can consult the book for the solution, as you might use a reference book.
This book is also of immense use as a learning tool. It covers issues faced every day by professional programmers. These issues are rarely covered in a typical programming book.
While many of the example programs solve complex problems, you
will find most of them to be relatively straightforward. If you
are still fairly new to programming, you will learn a lot just
by studying the example programs. They are fairly well-commented
and are quite readable.

