Read HTML The Definitive Guide Online
Authors: Chuck Musciano Bill Kennedy
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 known as applets perform simple tasks on the client computer, from responding to user mouse-or keyboard-actions to spicing up your HTML page displays with multimedia-enabling software.
You may embed applets in your HTML document 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 HTML content, interact with the user, validate form data, or even create windows and run entire applications independent of your HTML
pages. The possibilities are endless, and go far beyond the document model 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 HTML documents. We won't, however, even begin to pretend to teach you how to write and debug your own applet programs. This is a book about HTML, after all. Rather, get an expert opinion: turn to any of the many excellent texts from O'Reilly, including JavaScript: The Definitive Guide, Java in a Nutshell, and Exploring Java.
13.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 HTML display and multimedia innovations.
13.1.1 The HTML 4.0 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.0 standard calls inclusions. Like with images, the browser first loads the HTML document, then examines it for inclusions - additional, separate, and discrete content that are 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.
HTML 4.0 generally calls the inclusion contents objects. In fact, in your HTML document you may identify and load nearly any object file over the network through a universal
Once downloaded, the HTML standard dictates 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, plugins and other helper applications may provide the necessary rendering mechanism.
13.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.
13.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, or perform circuit simulation, or engage the user in a game, and so on.
13.1.1.3 Using applets correctly
An applet is nothing more than another tool you may 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.
13.1.1.4 Writing applets
Creating applets is a programming task, not usually a job for the HTML author, and certainly way 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 have caused some valid Java applets to fail when running on Internet Explorer. Hopefully, Microsoft will fix these problems 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 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 currently in widespread use.
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.
We should take this opportunity to mention also ActiveX, an alternative applet programming technology available only from Microsoft. ActiveX is proprietary, closely coupled to various versions of Microsoft Windows, and is fully functional 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 browser - specifically, Internet Explorer.
12.7 Named Frame or
13.2 Embedded Content
Window Targets