|
|
|
To mom and dad, who support unconditionally, c.w. liew, kas, spalding, phil lesh, and the mailing list. --Michael Girdley
To Thuy
--Brian Gloyer
|
Copyright © 1996 by Sams.net 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. Neither is any liability assumed for damages resulting
from the use of the information contained herein. For information,
address Sams.net Publishing, 201 W. 103rd St., Indianapolis, IN
46290.
International Standard Book Number: 1-57521-113-0
HTML conversion by :
M/s. LeafWriters (India) Pvt. Ltd.
Website : http://leaf.stpn.soft.net
e-mail :
leafwriters@leaf.stpn.soft.net
Publisher and President: | Richard K. Swadley |
Development Manager: | Dean Miller |
Managing Editor: | Cindy Morrow |
Director of Marketing: | John Pierce |
Assistant Marketing Managers: | Kristina Perry, Rachel Wolfe |
Acquisitions Editor | Grace M. Buechlein | Development Editor | Brian-Kent Proffitt |
Software Development Specialist | Cari Skaggs | Production Editor | Deborah Frisby |
Copy Editors | Miriam Bishop, Fran Blauw, Stacey Houston, Kristen Ivanetich, Howard Jones, Nanci Sears Perry | ||
Indexer | Tim Griffin | Technical Reviewer | Christopher Stone |
Editorial Coordinator | Bill Whitmer | Technical Edit Coordinator | Lynette Quinn |
Editorial Assistants | Carol Ackerman, Andi Richter, Rhonda Tinch-Mize | Cover Designer | Tim Amrhein |
Book Designer | Alyssa Yesh | Copy Writer | Peter Fuller |
Production Team Supervisor | Brad Chinn | ||
Production | Stephen Adams, Debra Bolhuis, Michael Brumitt, Kevin Cliburn, Jason Hand, Daniel Harris, Sonja Hart, Chris Livengood, Casey Price, Laura Robbins, Bobbi Satterfield, Mark Walchle |
All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams.net 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. Java is a trademark of Sun Microsystems, Inc.
A special acknowledgment goes to Lawrence Harris for his work on the table of contents of this book. Thank you!
Thanks to Brian Proffitt and to the rest of the gang at Sams.net. Most of all, thanks to Grace Buechlein, who called me on that May afternoon to talk about this Java book. Everyone has been very kind, and it is great to be involved with a team so committed to producing quality products.
The other authors and contributors to this book also worked hard to produce a high-quality, informative book. I was responsible only for about a half of this book, but it was originally going to be only a chapter or two. My portion just kept growing and growing. This book, above all, was a team effort of authors and editors, many of whom I've never met or spoken to. The efforts from all sides to produce Web Programming with Java are something I have a great deal of respect for, and I appreciate all the work people did to make my 50 percent read well.
I would also like to thank Professor Liew for providing my first introduction to the Java language, along with the rest of the faculty at Lafayette, including Professor Bjorling-Sachs and Professor Collins. Kelly Anne has also been great when I was unavailable to run. My employers at Southwest Research Institute were also great, even though I walked in some mornings so bleary-eyed from late-night writing that I could barely stand. And to Mom and Dad who bought that first Apple IIe fifteen Christmases ago. And to everyone else, friends, family, grandparents: thank you.
Michael Girdley is from San Antonio, Texas. He is currently pursuing a Bachelor of Science in computer science at Lafayette College in Easton, Pa. He is also the chief consultant at Allwilk Consulting (http://www.allwilk.com/), an organization specializing in Web site creation and Java programming. He is a member of the Lafayette College varsity swimming team and will earn his fourth varsity letter in 1996-1997. Michael hopes to find a job or go to graduate school after possibly graduating on time in May of 1997. He can be reached at girdleyj@allwilk.com and on the Web at http://www.lafayette.edu/~girdleyj/.
Kathryn A. Jones is a senior technical specialist in the New York office of Smith Barney, where she designs and develops NT-based client/server systems.
Jim Morey is a graduate student in pure mathematics at the University of British Columbia. He got his undergraduate degree from University of Guelph, Ontario. He is a hacker in the sense that he learns programming by hacking through other people's code. Catching the Java bug early, he hacked his way through the alpha version of Java and wrote "Pythagorus's Haven," a geometrical proof of the Pythagorean Theorem. This program won the Grand Prize in the Applet Programming Contest (http://java.sun.com/contest/results.html) sponsored by Sun. And recently, he won first place in the individual entertainment and games category in the Java Cup International (http://javacontest.sun.com/winners_circle/index.html) for CopyCat, a 3-D geometrical game. With all the computer equipment he has won, Jim will be in hacker heaven for quite some time at morey@math.ubc.ca or http://www.math.ubc.ca/~morey/.
Keith Orpen is a student of mathematics and coffee and lives in Vancouver. He can be pestered at korpen@math.ubc.ca, which is handy for sending him money and stuff. His favorite thing in the world is to answer Java newbie questions for free, especially if a ton of effort is involved.
Thomas Fredell is the consulting manager for the Atlanta office of Brainstorm Technologies, a leading provider of Groupware tools and consulting services. Thomas has the distinction of being the first graduate with a degree in cognitive science from the University of Virginia. His interests include software development using C, C++, and Java, and he is interested in language design and artificial intelligence. His noncomputer hobbies include skiing, sailing, squash, and chess. He can be contacted via e-mail at tfredell@braintech.com.
Brian Gloyer placed first in Sun's Java Applet Programming Contest with his Dining Philosophers applet. He is currently pursuing his Ph.D. at the University of California, Irvine. His research interests include artificial neural networks, image processing, and information systems. Some of his work, along with the original Dining Philosophers applet, can be seen on his home page at http://www.eng.uci. edu/~bgloyer.
Richard Lesh (rich@micros.umsl.edu) is an instructor with the Microcomputing Program at the University of Missouri, St. Louis. He has developed a variety of applications for the Macintosh, IBM pc, and various UNIX platforms. A number of software products that he has developed are in national distribution, including PLANMaker, a business plan building product, and a number of screen-saver modules published by Now Software in Now Fun! and by Berkeley Systems in After Dark.
George Reece (borg@imaginary.com) holds a philosophy degree from Bates College in Lewiston, Maine. He works as a consultant with York and Associates, Inc., and as a magazine columnist for the Java Developer's Journal. He has written some of the most popular MUD software on the Internet, include the Nightmare Object Library and the Foundation Object Library. For Java, he was the creator of the first JDBC implementation, the Imaginary JDBC Implementation for mSQL. His Internet publications include the free textbooks on the Lpc programming language, Lpc Basics and Intermediate Lpc.
As a reader, you are the most important critic of our books. We value your opinion and want to know what we're doing right, what we could do better, what areas you'd like to see us publish in, and any other words of wisdom you're willing to pass our way. You can help us make strong books that meet your needs and give you the computer guidance you require.
Do you have access to CompuServe or the World Wide Web? Then check
out our CompuServe forum by typing GO
SAMS at any prompt. If you prefer the World
Wide Web, check out our site at http://www.mcp.com.
Note: |
If you have a technical question about this book, call the technical support line at (800) 571-5840, ext. 3668. |
As the team leader of the group that created this book, I welcome
your comments. You can fax, e-mail, or write me directly to let
me know what you did or didn't like about this book-as well as
what we can do to make our books stronger. Here's the information:
FAX: | 317/581-4669 |
E-mail: | opsys_mgr@sams.mcp.com |
Mail: | Dean Miller Sams Publishing 201 W. 103rd Street Indianapolis, IN 46290 |
by Michael Girdley
Today you went Web "surfing" (a silly name, in my opinion), and you scoped out what's there. First you loaded up your favorite site (maybe one you created) and checked it out. Seems like everything's going fine-same white background, same images, nice logo-impressive, to say the least. You make your way around and wander into some home pages, possibly from a school somewhere or from one of the multitude of Web space providers. You might see pages like the one shown in Figure IN.1.
Figure IN.1. A page you might see.
Alternatively, you might visit one of the big commercial sites like ESPNet's Sportszone. You keep seeing references to Java everywhere, and you feel you're missing out.
So you search the Web for a Java-enhanced browser and eventually find one. Then you're off. Another world opens to you. Suddenly, the Web has started coming alive. You reach Gamelan (http://www.gamelan.com), a compendium of links to Java resources, and an amazing number of Java's wonders present themselves.
Or again, say you've seen all the hype (when it reaches Time magazine, it's hype). Of course, you want to use Java in your own pages, but how?
Web Programming with Java is your guidebook to creating, designing, and using Java for and on the World Wide Web.
You might be one of the people who should buy this book if you are excited and amazed by the extension that Java provides to the World Wide Web (also called WWW or "the Web"). You might have seen the amazing Java applets out on the Internet, and you are interested in learning how to do it yourself. Perhaps you've heard or seen the hype in magazines and periodicals, and your curiosity is aroused. But you don't know how to make your own Java programs (and you are not alone-I get many e-mails from people who want to know how). You need a source that will function both as a tutorial and as a reference for you to create your own Java applets.
This book provides you the capability to make your Web pages and sites come alive.
Besides serving as a tool to teach Java, this book will help programmers who are already familiar with Java and who are interested in the application of Java to the Web. This book covers Java completely, with special attention paid to its application on the WWW.
The most basic thing that a user of this book should have is access to a computer (pc or workstation) running on a Java-capable platform, to which the Java compiler and interpreter have been ported. At the time of this writing, many platforms are Java-capable. These range from the Sun SPARC workstations to the Microsoft Windows 95 interface to Steve Job's NeXT. A complete OS/2 port has already been completed and released by IBM, though many of the bugs are still being worked out at present.
The version of Sun's Java Developer's Kit (JDK) included in the CD-ROM that accompanies this book contains the full release of the Java language (1.x) for many different platforms.
If your platform isn't supported on the version included with
this book, don't worry. Chapter 2, "Getting
Started," covers the where and the how of getting the Java
Developer's Kit.
Caution |
The JDK is currently not ported to run under Microsoft Windows 3.1. As such, this book and the programs included will not be useful for 3.1 users. The JDK does work under Windows 95. If you haven't looked into Windows 95, this may be one more reason to consider it. |
If you have access to an appropriate system, what next? Most importantly, you should have an understanding of the major concepts of computer programming and some programming experience. As you will discover in this book, Java is an object-oriented language. Any previous experience you have in object-oriented design (OOD) will be a major bonus, though it is not necessary.
And finally, this is a book that concentrates on the use of Java on the World Wide Web. There are a number of differences between Java programming for the Web and for stand-alone Java programs. You'll see in this book that the difference between these two programming goals extends all the way to their names: stand-alone Java programs are called applications, but Java programs designed for the Web are called applets.
In conclusion, you should
This book covers the complete story of using Java on the Web. It contains all the information necessary to implement and create your very own Java applets and to effectively use all of the power of Java language. Web Programming with Java begins with an overview of the Java language and then deals progressively with more powerful and detailed concepts. Here is a summary of what each chapter contains.
Chapter 1, "An Overview of Java," is an overview of the Java language itself and some of the circumstances which prompted Sun Microsystems to create it. You will learn the why and how of Java as an object-oriented language. This object-oriented nature, you will discover, is key to the platform-independence of Java.
Chapter 2, "Getting Started," covers the major concepts you'll need to know to use Java on your platform. In this chapter, you'll learn about where to get the Java Developer's Kit and how to use it on your system. Finally, you'll see how to compile your Java programs and then view them using Sun's AppletViewer utility.
In Chapter 3, "An Introduction to Java Classes," you'll get a complete overview of what makes a Java class. The multiple classes and packages of classes available to you in the Java class library will all be covered. These include packages such as the java.net package, which enables network connections, and the java.awt package, which enables you to easily create GUIs.
Rounding out Part I, in Chapter 4, "Creating Your Own Objects," you'll get into creating your very own classes. You will learn how to subclass Java classes and also how to subclass your own classes. Finally, you will learn how Java handles Garbage Collection, which frees up system resources from where they are no longer needed.
Part II deals with the concept of building a complete Java applet. One of the most appealing aspects of the Web today is the graphical interaction between the user and source-the applet you create will do just that. Chapter 5, "Writing a Java Applet: The Order Entry System," begins Part II by discussing the first applet you will be creating, the Order Entry System. This chapter also describes exactly what applets entail in Java, and why.
Coverage of the Abstract Windowing Toolkit (AWT) will begin, with a description of the limits and capabilities of both applets and it. The control path of applets (init(), start(), etc.) will also be covered. Finally, you will learn exactly how to put applets into your Web pages.
Chapter 6, "The Order Entry System: Adding Features and Handling Events," begins with an explanation of the Java Abstract Windowing Toolkit, which is a package of classes that facilitate the inclusion and use of the typical user interface components, such as windows, pulldown menus, buttons, and dialog boxes. You'll look into the use and description of the AWT concepts of containers and components. Part of this chapter will cover the implementation of some of the components. The graphical components of the AWT will be covered in this chapter, while others will be left for later chapters. This chapter also covers some of the limitations of applets and compares them with Java applications.
Chapter 7, "The Order Entry System: Entry Fields, Labels, and Validation," covers the remaining components, including those specific to text input and output. There are multiple components dealing with text, including Labels, Text Fields, and Text Areas. The implementation and function of each one of these components is detailed in this chapter. This chapter also takes some more steps toward completing the Order Entry System. You will extend the System to include the components discussed in this chapter and to handle them appropriately. You will also fill in some of the "holes," such as the unfinished updateValues method, and you will activate the "Clear" button. To finish Chapter 7, you will learn the methods of password entry and identification, which are very important in the applet design business. This same section also covers data validation in the entry fields in applets.
Chapter 8, "The Order Entry System: Managing the Applet Layout," is a monster. First, you are going to further explore the concepts of containers in the Java AWT. The chapter covers how to implement the different container types and how they interact with each other. The different containers available enable you to create stand-alone window applets, and also enable you to clean up the appearance of the Order Entry System. I will also describe pop-up dialog boxes and the means to incorporate them into your applets.
Next, Chapter 8 covers the five different AWT Layout Managers. These enable you, the programmer, to place your components in containers in an orderly fashion, while still maintaining platform independence. The five layout managers are: Flow Layout, Border Layout, Card Layout, Grid Layout, and the GridBag Layout. The GridBag Layout is the layout manager you will use to reorganize the Order Entry System applet panel.
Chapter 9, "The Order Entry System: Adding Graphics and a Logo," covers some of the most exciting aspects of Java, those dealing with graphics. I will discuss the AWT Graphics class, which enables you to implement many different graphical items. These features involve drawing, displaying images, and setting colors, among others. I will discuss the Canvas class, which is a special type of component designed for use in dealing with graphics. The Order Entry System applet will also be extended further to include the concepts dealt with in this chapter.
Many languages do not have built-in capabilities to enable you to efficiently detect and deal with errors, mishaps, and events out of the ordinary. Luckily, Java includes a means to handle these exceptions to the norm simply and effectively. You will learn this process of handling exceptions and errors. In other words, although you plan on a certain progression through your code, you will also plan to implement code that will cover the instances where things don't go your way. The handling of exceptions and errors, and the command structures to implement them, will be the focus of this chapter.
Chapter 11, "Reading and Writing with Java," covers the implementation of reading and writing with Java. This process in Java is centered around the concept of streams. Just as a stream of water flows in one direction, starting and ending, so does a stream of data. Streams simply are linear paths that connect a data producer and a data consumer together to allow the serial (one chunk after another) transmission of data. Streams can connect many different things. For example, a stream can connect two independent processes together; they can connect a class to a file or even connect your class to a network. This chapter will cover in depth the usage of the multiple types of streams available in the Java-standard libraries.
Chapter 12, "Network Programming with Java," begins the third and final part of this book. Where the first part introduced the Java language, and the second part covered basic applet techniques, the chapters in this section cover more complicated and powerful applet techniques. Chapter 12 covers network programming with Java. The examples in this chapter rely extensively on the java.net package in the Java class library in order to make, manage, and utilize the network capabilities of Java. You'll learn how to have your applets and applications in Java make connections across LANs and the Internet. And most importantly, you'll learn the process of connecting from your applet to your Web server. The security limitations placed on applets will be discussed. You will develop an on-line inquiry applet, which will demonstrate these concepts.
Chapter 13, "General Purpose Classes," covers the different general purpose classes available in the Java class library and demonstrates their use. Then, Chapter 14, "Extending Java," covers extending Java to interface with other languages, including C applications. Chapter 15, "Interfacing with a Database: Catalog Applet," is about interfacing with databases in Java. It covers the process of making a network connection to a database and then creating an on-line catalog applet.
Chapter 16, "Multithreading with Java," covers one of the most important features of Java: multithreading. It is a complicated topic, which will prove to be one of the most important and powerful features of Java. Chapter 17, "Advanced Graphics: Multimedia," covers multimedia in Java applications. You'll learn how to create your own multimedia applet.
Chapter 18, "Serious Play: Game Applets," covers game programming in Java. The process of creating game elements will be discussed. You will learn about the implementation of those elements into an example applet. Finally, Chapter 19, "Security Issues," covers the future of Java and security implementations of Java.
This book uses the following conventions: