home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeHTML & XHTML: The Definitive GuideSearch this book

Chapter 12. Executable Content

One of the most exciting recent developments in web technologies is the ability to deliver applications directly to the user's browser. These typically small programs, which are known as applets, perform simple tasks on the client computer, from responding to user mouse- or keyboard-actions to spicing up your web page displays with multimedia-enabling software.

You may embed applets into your documents using a special programming language known as JavaScript. Or you can load and execute Java-based, platform-independent applets over the Internet. During execution, these programs may generate dynamic content, interact with the user, validate form data, or even create windows and run entire applications independent of your pages. The possibilities are endless, and they go far beyond the document model that was originally envisioned for HTML.

In this chapter, we show you, with simple examples, how to embed and include executable content -- scripts and applets -- in your documents. We won't, however, even begin to pretend to teach you how to write and debug your own applets. This is a book about HTML and XTHML, after all. Rather, get an expert opinion: turn to any of the many excellent texts from O'Reilly, including JavaScript: The Definitive Guide, by David Flanagan, Java in a Nutshell, by David Flanagan, and Exploring Java, Pat Niemeyer and Josh Peck.

12.1. Applets and Objects

Applets, like client-side image maps, represent a shift in the basic model of web communications. Until recently, servers performed most of the computational work on the Web, client browsers being not much more than glorified terminals. With applets, web technology is shifting toward the client, distributing some or all of the computational load from the server to the client and its browser.

Applets also represent a way of extending a browser's features without forcing users to purchase or otherwise acquire a new browser, as is the case when developers implement new tag and attribute extensions to HTML. Nor do users have to acquire and install a special application, as is required for helper or plug-in applications. This means that once users have a browser that supports applets (Netscape and Internet Explorer do), you can deliver applets immediately, including display and multimedia innovations.

12.1.1. The Object Model

Java-based applets -- web page-referenced programs retrieved from a network server and executed on the user's client computer -- actually are a subset of what the HTML 4 and XHTML standards call inclusions. As with images, the browser first loads the HTML document, then examines it for inclusions -- additional, separate, and discrete content that is to be handled by the client browser. A GIF image is one type of inclusion. A .wav sound file is another; an MPEG movie is another; a Java-based clock program is another.

The HTML 4 and XHTML standards generally calls the inclusion contents objects. In fact, in your document you may identify and load nearly any object file over the network through a universal <object> tag, which we discuss in detail later in this chapter. Section 12.2.1, "The <object> Tag"

Once downloaded, the standards dictate that the browser somehow render the object, by internal or external mechanisms. The popular graphical browsers, for instance, have integrated software for displaying GIF and JPEG images. Otherwise, plug-ins and other helper applications may provide the necessary rendering mechanism.

12.1.1.1. The applet model

With applet-based objects, the browser sets aside a portion of the document display space. You may control the size and position of this display area; the applet controls what is presented inside.

The applet object is software, an executable program. Accordingly, besides providing a display space, the browser, in tandem with the client computer environment and resources, provides the applet runtime environment -- typically Java.

During execution, the applet has access to a restricted environment within the user's computer. For instance, applets have access to the mouse and keyboard and may receive input from the user. They can initiate network connections and retrieve data from other servers on the Internet. In sum, applets are full-fledged programs, complete with a variety of input and output mechanisms, along with a full suite of network services.

Several applets may be placed in a single document; they all execute in parallel and may communicate with each other. While the browser may limit their access to its computer system, applets have complete control of their virtual environment within the browser.

12.1.1.2. The applet advantage

There are several advantages of applets, not the least of which is providing more compelling user interfaces within a web page. For instance, an applet might create a unique set of menus, choices, text fields, and similar user-input tools different from those available through the browser. When the user clicks a button within the applet's interaction/display region, the applet might respond by displaying results within the region, signaling another applet, or even by loading a completely new page into the browser.

We don't mean to imply that the only use of applets is to enhance the user interface. An applet is a full-fledged program that can perform any number of computational and user-interactive tasks on the client computer. An applet might implement a real-time video display, perform circuit simulation, engage the user in a game, provide a chat interface, and so on.

12.1.1.4. Writing applets

Creating applets is a programming task, not usually a job for the HTML or XHTML author, and certainly well beyond the scope of this book. For details, we recommend you consult any of the many applet programming texts that have recently appeared on bookshelves everywhere, including those from O'Reilly & Associates.

Today, one language dominates the applet programming world: Java. Developed by Sun Microsystems of Mountain View, California, Java supports an object-oriented programming style wherein classes of applets can be used and reused to build complex applications.

By invention, applets built from the same language should run with any browser that supports them. In reality, certain Microsoft implementation decisions had caused some valid Java applets to fail when running on earlier versions of Internet Explorer. Microsoft has fixed these problems with Internet Explorer Version 5 and Java will remain a universal programming language for the Web. In any case, the conscientious Java programmer should keep abreast of the latest technology and create applets that are certifiably 100% pure Java. Microsoft, in particular, is trying to get programmers to use proprietary extensions to Java that will work on only Microsoft platforms and is refusing to support key parts of the standard. We recommend avoiding any vendor extensions to Java that deviate from the standard Java 1.1 version.

You can shield yourself from platform dependencies by using the Java Plugin from Sun; see http://java.sun.com/products/plugin. There are versions for both Internet Explorer and Netscape Navigator. Currently, the plugin is the only way to get support for the latest version of Java (Java 2 SDK 1.3).[66]

[66] The plugin has achieved some acceptance for running Java 2 applets in intranet (i.e., corporate network) environments, but we have yet to see an applet on the public Internet that required Java 2.

We should take this opportunity to also mention ActiveX, an alternative applet programming technology available from Microsoft. ActiveX is proprietary, closely coupled with various versions of Microsoft Windows, and works only when used with Internet Explorer. ActiveX applets will run on versions of Internet Explorer targeted to various versions of Windows, but a single ActiveX applet will not run on these different versions without recompilation. This is in contrast with Java applets, where a single Java applet can be written and compiled once and immediately run on a broad range of browsers and operating systems.

ActiveX also presents an unacceptably high security risk to any user whose browser supports ActiveX technology. It is ridiculously easy to penetrate and damage a computer running a browser that allows ActiveX applets to be executed. For this reason, we cannot recommend ActiveX as a viable applet implementation technology and we go so far as to recommend that users disable ActiveX capability within their browsers -- specifically, Internet Explorer.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.