Core Web Programming (With CD-ROM)
Authors: Marty Hall
Venue:  (1997)

From the Book:PREFACE: In late 1995, I proposed a new course for the Johns Hopkins University's part-time graduate program in Computer Science, for which I had been teaching part-time for a number of years. My idea was to bring together the major Web-related topics in a single course dubbed “Distributed Development on the World Wide Web,” with Java as a unifying theme. We'd look at HTML, Java, HTTP, CGI Programming, and JavaScript, with lots of hands-on projects and no exams. Little did I know what I was getting myself into. By the time I taught the first section in the summer of 1996, the Java tidal wave had swept through the university and the employers that the students represented. The class filled shortly after enrollment opened. There were more students on the waiting list than in the course. I got frantic phone calls from students insisting that they absolutely had to be in the course. Several local companies called asking for on-site courses. What fun! I even found myself switching to Java for the majority of the software development in my “real” job. However, when I went shopping for texts over the next semester or two, I got a rude surprise. Despite the availability of good books in most of the areas I wanted to cover, I found that I needed three, four, or even five separate books to get good coverage of the material I wanted for the course. Similarly, for my day job, I was constantly switching back and forth among the best of the huge stack of books I had accumulated and the various on-line references. Surely there was a better way. Shouldn't it be possible to fit 95 percent of what professional programmers use in about 35 percent of the space, and getitall in one book? Well, about that time I struck up an acquaintance with Gary Cornell, author of Core Java, and he suggested that I use my class notes as the outline for a World Wide Web developer's reference, and that I entitle it Core Web Programming. Well, after a new version of Java, two new versions of JavaScript, several new browser releases, and a year or two of full-time Web development and Java hacking, here it is. I hope you find it useful. Real Code for Real Programmers This book is aimed at serious software developers. If you are looking for a book that shows you how to use a browser, lists the current hottest Web sites, and pontificates about how Java will break the Microsoft monopoly, you've come to the wrong place. If you're already a programmer of some sort and want to get started with HTML, Java, CGI, and JavaScript as quickly as possible, this is the book for you. I'll illustrate the most important approaches and warn you of the most common pitfalls. In order to do so, I will include plenty of working code: over 250 complete Java classes, for instance. I've tried to give detailed examples of the most important and frequently used features, summarize the lesser used ones, and refer you to the API (available on-line) for a few of the rarely used ones. A word of caution, however. Nobody becomes a great developer just by reading. You have to write some real code too. The more, the better. In each chapter, I suggest that you start by making a simple program or a small variation of one of the examples given, then strike off on your own with a more significant project. Skim the sections you don't plan on using right away, then come back when you are ready to try them out. If you do this, you should quickly develop the confidence to handle the real-world problems that brought you here in the first place. You should be able to balance the demand for the latest features in Web pages with the need for multi-platform support. You should be comfortable with frames, style sheets, and layered HTML. You should have no qualms about making Web interfaces to your corporate database, either via CGI or through JDBC. You should be able to make portable stand-alone graphical applications in Java 1.0 or 1.1. You should be able to connect these applications to remote systems over the network. You should be able to easily distribute computation among multiple threads, or even spin it off to separate systems using RMI. You should be able to use JavaScript to validate CGI forms or to animate Web pages, while still supporting older browsers. You should get a raise. How This Book Is Organized The book is divided into four parts, as follows: Part I: The HyperText Markup Language Web pages are created using HTML, the HyperText Markup Language. HTML lets you mix regular text with special tags that describe the content, layout, or appearance of the text. These tags are then used by Web browsers like Netscape Navigator or Microsoft Internet Explorer to format the page. This first part of the book will cover the following topics in HTML: HTML 3.2. Full coverage of all the elements in the latest official HTML standard. Hypertext links, fonts, images, tables, client-side image maps, and more. Major Netscape and Internet Explorer extensions. Forwarding pages, using custom colors and font faces, embedding audio, video, and Activex components. Knowing when extensions make your pages non-portable. Frames. Dividing the screen into rectangular regions, each associated with a separate HTML document. Borderless frames. Floating frames. Targeting frame cells from hypertext links. Solving common frame problems. Cascading Style Sheets. Level one style sheets for customizing fonts, colors, images, text formatting, indentation, lists, and more. Layers. Using extended style sheets or the LAYER and ILAYER elements for dynamic, layered HTML. Part II: Java Java is a powerful general-purpose programming language that can be used to create stand-alone programs as well as ones that are embedded in Web pages. Java topics that are covered include: Unique features of Java. What's different about Java? The truth about Java myths and hype. Object-oriented programming in Java. Variables, methods, constructors, overloading, and interfaces. Modifiers in class declarations. Packages and the CLASSPATH. Java syntax. Primitive types, operators, strings, vectors, arrays, input/output and the Math class. Graphics. Applets. Applications. Drawing, color, font, and clipping area operations. Loading and drawing images. Mouse and keyboard events. Processing events in Java 1.02. Handling them in Java 1.1. Event types, event listeners, and low-level event handlers. Inner classes. Windows. Canvas, Panel, Applet, ScrollPane, Frame, Dialog, FileDialog, and Window. Component and Container. Lightweight components and containers in Java 1.1. Saving and loading windows using object serialization. Layout Managers. FlowLayout, BorderLayout, GridLayout, CardLayout, and GridBagLayout. Positioning components by hand. Strategies for using layout managers effectively. Writing your own layout manager. GUI Controls. Buttons, checkboxes, radio buttons, combo boxes, list boxes, textfields, text areas, labels, scrollbars, and popup menus. Creating slider and image button classes. Threads. Threads in separate or existing objects. Synchronizing access to shared resources. Grouping threads. Multi-threaded graphics and double buffering. Client-server programming. Clients and servers using sockets. The URL class. An HTTP client and server. Remote Method Invocation (RMI). Java DataBase Connectivity (JDBC). Part III: CGI Programming Web pages can be connected to databases and other programs on the server by means of the Common Gateway Interface (CGI). Part III covers the following CGI areas: HTTP. Request types. Request headers. Response headers. Cookies. Public-key cryptography. The client side. HTML forms and form elements. Using applets to talk to CGI programs. Sending via GET and POST. Bypassing the HTTP server and talking directly to programs from applets. The server side. Reading GET and POST data. CGI environment variables. Manipulating cookies. Java classes to decode and parse CGI data. Server-side Java and the servlet API. CGI alternatives. Part IV: JavaScript JavaScript is a scripting language that can be embedded in Web pages and interpreted as the pages are loaded. The final part will cover the following topics in JavaScript: JavaScript syntax. Fields, methods, functions, strings, objects, arrays, and regular expressions. Customizing Web pages. Adapting to different browsers, JavaScript releases, and screen sizes. Making pages dynamic. Animating images. Manipulating layers. Responding to user events. Validating CGI forms. Checking form entries as they are changed. Checking data when form is submitted. Handling cookies. Reading and setting values. The Cookie object. Controlling frames. Sending results to specific frames. Preventing documents from being framed. Updating multiple frame cells. Giving frame cells the focus automatically. Integrating Java and JavaScript. LiveConnect and the JSObject class. JavaScript quick reference. Major classes in JavaScript 1.0, 1.1, and 1.2. All fields, methods, and event handlers. Document, Window, Form, Element, String, Math, RegExp, and so forth. Conventions Throughout the book, concrete programming constructs or program output will be presented in a monospaced font. For example, when abstractly describing Java programs that can be embedded in Web pages, I will refer to “applets,” but when I say Applet I am talking about the specific Java class from which all applets are derived. User input will be indicated in boldface, and command-line prompts will either be generic (“Prompt>” ) or indicate the operating system to which they apply (“Unix>” ). For instance, the following indicates that “Some Output” is the result when “java SomeProgram” is executed: Prompt> java SomeProgram Some Output Important standard techniques are indicated using specially marked entries, as in the following example: Core Approach Pay particular attention to items in “Core Approach” sections. They indicate techniques that should always or almost always be used. Notes and warnings will be called out in a similar manner. About the CD-ROM To get you started quickly, the CD-ROM contains the following elements formatted for Windows 95/NT and Macintosh platforms. For other operating systems, please see the download sites indicated throughout the book. An HTML guide to all resources given in the book, organized by chapter. This includes: More than 200 HTML and JavaScript documents used throughout the text. You can view them on-line, examine their source code, and adapt them for your projects. Complete source code for more than 250 Java classes listed or used in various chapters. You can freely use or adapt these classes. On-line versions of all Java applets. Hypertext links to all URLs listed in the book. The Java Developer's Kit, version 1.02. The Java Developer's Kit, version 1.1. The WinEdit text editor, customized for Java programming (Windows 95/NT only). Microsoft Internet Explorer version 3. I