One of the most useful web technologies is the ability to deliver applications directly to the browser. These typically small programs 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 can embed scripts in your documents using a language known as JavaScript. Or you can load and execute small, Java-based, platform-independent applications known as applets. 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 simple document model originally envisioned for HTML.
In this chapter, we show you, with simple examples, how to include two kinds of executable content—scripts and applets—in your documents. We won't, however, teach you how to write and debug executable content. 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, especially the companion JavaScript: The Definitive Guide, by David Flanagan.
Applets represent a shift in the basic model of web communications. In most other web applications, servers perform most of the computational work, client browsers being not much more than glorified terminals. With applets, web technology shifts to the client, distributing some or all of the computational load from the server to the client computer and its browser.
Applets also represent a way of extending a browser's features without forcing users to acquire new browsers, 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 (all the currently popular ones do), you can deliver applets directly to the browser, including display and multimedia innovations.
Java-based applets—web page-referenced programs retrieved from a network server and executed on the user's client computer—are an example 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 the client browser is to handle. A GIF image is one type of inclusion. A .wav sound file, an MPEG movie, and a Java-based clock program are other types.
The HTML 4 and XHTML standards generally call 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, discussed in detail
shortly in section
12.2.1.
Once the object has been downloaded, the standards dictate that the browser somehow render the object, by internal or external mechanisms. Otherwise, plug-ins and other helper applications may provide the necessary rendering mechanism. Internet Explorer, for example, has its internal resources play an AVI movie, whereas other browsers rely upon some third-party software, such as RealPlayer or QuickTime, to show the movie.
With Java applets, 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 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 with a runtime environment called a virtual machine.
During execution, Java applets have access to a restricted environment within the user's computer. For instance, applets have access to the mouse and keyboard so that they may receive input from the user. Depending on the security policy in place, applets also may 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.
You may place several applets 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.
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 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.
An applet is nothing more than another tool you can use to produce compelling and useful web pages. Keep in mind that an applet uses computational resources on the client to run and therefore places a load on the user's computer. It can degrade system performance.
Similarly, if an applet uses a lot of network bandwidth to accomplish its task (a real-time video feed, for example), it may make other network communication unbearably slow. While such applications are fun, they do little more than annoy your target audience.
To use an applet correctly, balance the load between the browser and the server. For each page, decide which tasks are best left to the server (forms processing, index searches, and the like) and which tasks are better suited for local processing (user-interface enhancements, real-time data presentation, small animations, input validation, and so on). Divide the processing accordingly. Remember that many users have slower network connections and computers than you do, and design your applets to satisfy the majority of your audience.
Used the right way, applets seamlessly enhance your pages and provide a satisfying experience for your audience. Used improperly, applets are just another annoying bandwidth waster, alienating your users and hurting your pages.
Creating Java applets is a programming task, not usually a job for the HTML or XHTML author. For details, we recommend that you consult any of the many Java programming texts, including those from O'Reilly.
Developed by Sun Microsystems, Inc. of Mountain View, California, Java supports an object-oriented programming style wherein classes of applets can be used and reused to build complex applications. One would think that applets written in the same language should run in any browser that supports Java. As is so often the case, reality is more complex. Until Netscape 6 and Internet Explorer 6, browsers included their own Java Virtual Machines (JVMs), and their implementations, especially Microsoft's, could be quirky. Certain Microsoft implementation decisions in Internet Explorer 4 and earlier caused some valid Java applets to fail when running. Microsoft fixed these problems with Internet Explorer version 5 but, because of its lawsuit with Sun, chose not to include a JVM in Internet Explorer 6.[*] Although this may sound like bad news for applets, in fact, Internet Explorer 6 prompts you to download Microsoft's JVM. Sun's Java Plug-in is free over the Internet. Users of any browser can install the Java Plug-in to get state-of-the-art Java support.
We should take this opportunity also to mention ActiveX, an alternative executable content technology originally developed by Microsoft. ActiveX itself is proprietary, closely coupled with various versions of Microsoft Windows, and Microsoft's plug-in works only when used with Internet Explorer, though alternative plug-in implementations now exist for all the popular browsers.
ActiveX controls (as they are called) run on browser versions targeted to various versions of Windows, but a single ActiveX control will not run on these different versions without recompilation. This is in contrast with Java applets; 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. [†] Though over the years security has gotten better, it is ridiculously easy to penetrate and damage a computer running a browser that allows ActiveX applets to be executed. In fact, all the popular browsers, Internet Explorer included, let users explicitly block ActiveX applets. 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.
[*] Actually, Internet Explorer 6 users must download and install Java support. Read on for details.
[*] As we wrote this, even this situation may change, with Microsoft reversing itself and deciding to include a JVM in a service pack for Windows XP. There is still no sign of default inclusion of a JVM in Internet Explorer 6 downloads, however.
[†] You can find a good description of the risks at http://www.digicrime.com/activex.