本书完全由实际的Java程序实例构成,内容涉及Java 1.3,包含了164个完整、实用的实例:超过17900行的代码注释涵盖了20种不同的JavaAPI,包括servlet、JSP、XML、Swing和Java 2D。最畅销的《Javaina Nutshell》的作者亲自编写了本书所有的实例代码,读者可以从中学习Java语言,也可以对这些代码进行修改为自己所用。如果你喜欢按照实例去学习,那么本书正是你的最佳选择!
本书是《Java in a Nutshell》、《Java Foundation Classes in a Nutshell》和《Java Enterprise in a Nutshell》的姐妹篇。尽管这三本书属于速查手册,但其中仍然对各种Java主题提供了速成指南。而本书则是弥补这些书所遗漏的一些内容,为Java新手提供了一系列的实例代码,当然,这些代码对于那些经验丰富的编程老手来说也同样适用。本书并没有限制读者的发挥,或仅仅对Java的语法或方法调用进行详细的解释,而是展示了一些具有完美注释的优秀代码实例,从而帮助读者最大程度地认识Java。本书的每一章都包含了编程练习,有助于读者巩固所学到的知识。
本书涵盖的主题包括:
· 用来阐述基本Java功能和关键APl的实例,包括I/O、线程、网络、安全、映像和串行化。
· 使用图形用户界面和Java图形特性的程序,重点介绍的是Swing、Java 2D、打印、数据传输、JavaBeans和appletAPl。
· 用来解释关键性企业级API的实例,包括远程方法调用、数据库连接、servlet、JSP和XML。
· 本书最后附带了一个实例索引,使读者能够方便地找到编程概念或Java类,也能够方便地找到用于阐述某个概念或使用了某个类的实例。
Preface
Part I: Core java APIs
Chapter 1 -- Java Basics
Hello World
FizzBuzz
The Fibonacci Series
Using Command-Line Arguments
Echo in Reverse
FizzBuzz Switched
Computing Factorials
Recursive Factorials
Caching Factorials
Computing Big Factorials
Handling Exceptions
Interactive Input
Using a StringBuffer
Sorting Numbers
Computing Primes
Exercises
Chapter 2 -- Objects, Classes, and Interfaces
A Rectangle Class
Testing the Rect Class
A Rect Subclass
Another Subclass
Complex Numbers
Computing Pseudo-Random Numbers
Computing Statistics
A Linked List Class
Advanced Sorting
Exercises
Chapter 3 -- Input/Output
Files and Streams
Working with Files
Copying File Contents
Reading and Displaying Text Files
Listing Directory and File Information
Compressing Files and Directories
Filtering Character Streams
Filtering Lines of Text
A Custom HTML Output Stream
Exercises
Chapter 4 -- Threads
Thread Basics
Threads and Thread Groups
Deadlock
Timers
Exercises
Chapter 5 -- Networking
Downloading the Contents of a URL
Using a URLConnection
Sending Email Through a URLConnection
Connecting to a Web Server
A Simple Web Server
A Proxy Server
Networking with Applets
A Generic Client
A Generic Multithreaded Server
A Multithreaded Proxy Server
Sending Datagrams
Receiving Datagrams
Exercises
Chapter 6 -- Security and Cryptography
Running Untrusted Code
Loading Untrusted Code
Message Digests and Digital Signatures
Cryptography
Exercises
Chapter 7 -- Internationalization
A Word About Locales
Unicode
Character Encodings
Handling Local Customs
Localizing User-Visible Messages
Formatted Messages
Exercises
Chapter 8 -- Reflection
Obtaining Class and Member Information
Invoking a Named Method
Exercises
Chapter 9 -- Object Serialization
Simple Serialization
Custom Serialization
Externalizable Classes
Serialization and Class Versioning
Serialized Applets
Exercises
Part II: Graphics and GUIs
Chapter 10 -- Graphical User Interfaces
Components
Containers
Layout Management
Event Handling
A Complete GUI
Actions and Reflection
Custom Dialogs
Displaying Tables
Displaying Trees
A Simple Web Browser
Describing GUIs with Properties
Themes and the Metal Look-and-Feel
Custom Components
Exercises
Chapter 11 -- Graphics
Graphics Before Java 1.2
The Java 2D API
Drawing and Filling Shapes
Transforms
Line Styles with BasicStroke
Stroking Lines
Filling Shapes with Paint
Antialiasing
Combining Colors with AlphaComposite
Image Processing
A Custom Shape
Custom Strokes
Custom Paint
Advanced Animation
Displaying Graphics Examples
Exercises
Chapter 12 -- Printing
Printing with the Java 1.1 API
Printing with the Java 1.2 API
Printing Multipage Text Documents
Printing Swing Documents
Exercises
Chapter 13 -- Data Transfer
Data Transfer Architecture
Simple Copy-and-Paste
A Transferable Data Type
Cutting and Pasting Scribbles
Dragging and Dropping Scribbles
Exercises
Chapter 14 -- JavaBeans
Bean Basics
A Simple Bean
A More Complex Bean
Custom Events
Specifying Bean Information
Defining a Simple Property Editor
Defining a Complex Property Editor
Defining a Bean Customizer
Exercises
Chapter 15 -- Applets
Introduction to Applets
A First Applet
A Clock Applet
Applets and the Java 1.0 Event Model
Java 1.0 Event Details
Reading Applet Parameters
Images and Sounds
JAR Files
Exercises
Part III: Enterprise Java
Chapter 16 -- Remote Method Invocation
Remote Banking
A Bank Server
A Multiuser Domain
Remote MUD Interfaces
The MUD Server
The MudPlace Class
The MudPerson Class
A MUD Client
Advanced RMI
Exercises
Chapter 17 -- Database Access with SQL
Accessing a Database
Using Database Metadata
Building a Database
Using the API Database
Atomic Transactions
Exercises
Chapter 18 -- Servlets and JSP
Servlet Setup
A Hello World Servlet
Servlet Initialization and Persistence: A Counter Servlet
Database Access with Servlets
A Login Screen with JSP
Request Forwarding
JSP Pages and JavaBeans
Ending a User Session
Custom Tags
Deploying a Web Application
Exercises
Chapter 19 -- XML
Parsing with JAXP and SAX 1
Parsing with SAX 2
Parsing and Manipulating with JAXP and DOM
Traversing a DOM Tree
Traversing a Document with DOM Level 2
The JDOM API
Exercises
Chapter 20 -- Example Index
Index
This book is a companion volume to my previous books Java in a Nutshell, Java Foundation Classes in a Nutshell, and Java Enterprise in a Nutshell. While those books are quick-references at heart, they also include accelerated introductions to various JavaTM programming topics and small sets of example programs. I wrote Java Examples in a Nutshell to pick up where those books leave off, providing a suite of example programs for novice Java programmers and experts alike.
This book was a lot of fun to write. The first edition came about when Java 1.1 was released at more than double the size of Java 1.0. While I was busy writing additional examples for the second edition of Java in a Nutshell, the engineers at Sun were busy turning Java into something that could no longer quite fit in a nutshell. With its quick-reference section expanding so much, Java in a Nutshell could no longer hold many examples. We were able to include some examples of new Java 1.1 features, but we had to cut many more than we could include. This was a hard decision; the examples in Java in a Nutshell were one of its most popular features.
This book is the result of those cuts, and I am glad that we made the decision we did. Given the freedom to devote an entire book to examples, I was able to write the examples I really wanted to write. I was able to go into more depth than I ever would have before, and I found myself really enjoying the exploration and experimentation that went into developing the examples. For the second edition of the book, I had the pleasure of exploring and experimenting with new parts of the ' Java API: SwingTM, Java 2DTM, servlets, and XML. I hope you will use these examples as a starting point for your own explorations, and that you get a taste of the same excitement I felt while writing them.
As its name implies, this book teaches by example, which is how many people learn best. There is not a lot of hand-holding, nor will you find detailed documentation of the exact syntax and behavior of Java statements. This book is designed to work in tandem with Java in a Nutshell, .Java Foundation Classes in a Nutshell, and Java Enterprise in a Nutshell. You'll probably find those volumes quite useful while studying the examples here. You may also be interested in the other books in the O'Reilly Java series. Those books are listed at http://java.oreilly.com.
This book is organized into three parts. Chapters 1 through 9 cover the core nongraphical parts of the Java APl. The APIs covered in these chapters are docu mented in Java in a Nutshell. Chapters 10 through 15 form the second part of the book. These chapters demonstrate Java's graphics and graphical user interface APIs, which are documented in Java Foundation Classes in a Nutshell. Finally, Chapters 16 through 19 contain examples of Java enterprise APIs and complement the book Java Enterprise in a Nutshell.
You can read the chapters in this book in more or less whatever order they strike your interest. There are some interdependencies between the chapters, however, and some chapters really ought to be read in the order they are presented. For example, it is important to read Chapter 3, Input/Output, before you read Chapter 5, Networking. Chapter 1, Java Basics, and Chapter 2, Objects, Classes, and Interfaces, are aimed at programmers just starting out with Java. Seasoned Java programmers will probably want to skip them.