Chapter 31

Developing Applets


CONTENTS


In this chapter you will learn what an applet is, how it relates to HTML, and what some of the possible uses of applet technology are. By the time you finish this chapter, you will have an understanding of what an applet is, how it works, and what it can do.

How Applets Work

Applet technology is the driving force behind the intensity of the Java revolution. Applets enhance Web documents with animation, sound, and other special effects. However, applets are just standard Java programs with a few special hooks into the Web browser's environment. The capability of applets to take advantage of the resources provided by a Web browser's environment is what allows them to be easily and powerfully integrated within Web pages.

Figure 31.1 describes the steps an applet takes to display itself in the browser's window.

Figure 31.1 : How a Java applet works.

As the figure shows, the browser makes a request to the loader to fetch the applet specified in the document's HTML. After the applet has been fetched, the applet begins to execute. The applet is executed by the Java runtime interpreter attached to the browser. The browser acts as a conduit between the Java Virtual Machine inside the interpreter and the outside user interface.

The Applet class provides an application framework and tools to access the facilities provided by the browser. Via the browser, the applet has access to graphics, sound, and network capabilities. The Applet class can be viewed as merely a wrapper around the capabilities provided by the browser.

The Relationship Between HTML and Applets

An applet is like a child application of the browser. The browser launches the applet in a predefined environment inside the browser. In turn, the browser obtains the information pertaining to the applet's environment from the current document's HTML. In this sense, the relationship between HTML and an applet is that of a command line executing a program.

From within HTML, the syntax to specify the execution of an applet is provided by the applet and parameter tags as discussed in Chapter 16, "Web Programming with the java.applet Package."

The applet tag provides all the information needed to launch the applet. Everything from the base directory to command-line parameters can be specified from within this tag. Here's an example:

<APPLET CODE = "lights.class" width=400 height=75 align=center >
<PARAM NAME="text" VALUE="Blink">
<BLOCKQUOTE>
<HR>
If you were using a Java(tm)-enabled browser,
you would see blinking lights instead of this paragraph.
<HR>
</BLOCKQUOTE>
</APPLET>

From this HTML command line, the browser is told how to launch the lights.class file with the parameter text. The HTML merely specifies a command line of sorts to the browser.

Applets and Interactive Web Pages

Usually, when the word applet is mentioned, the World Wide Web is not far out of mind. There is good reason for this. An applet is a Java program designed to run in the environment provided by a Web browser. The last statement might seem a little simplistic, but take a second to think what that environment provides to an application.

Inside the browser, an application has the capability to display images, play audio files, and access the Internet. The Applet class provides methods to tap these resources provided by the Web browser.

Because applets are executed locally on your machine, you are able to interact with the applet as part of the Web page's display. Other Web applications that rely on CGI programs (see Chapter 32, "Integrating Applets into Distributed Applications") do not provide interactivity at the local level. Instead, your browser must send information-processing requests to programs that are located on remote Web servers. Besides performance impacts, this remote processing approach also suffers from the difficulties involved in maintaining information about the state of the applications it supports. The Java model of local execution is able to support a high degree of interactivity. All state information is maintained within the local browser environment and is not distributed between the browser and Web server.

Applets Versus Scripts

As discussed previously, applets are launched from within an HTML command. After an applet is launched, it operates in a well-defined area of the browser window and is restricted from altering the state or content of the current HTML document. Scripts written in the JavaScript language are not placed under these restrictions and have more latitude in modifying the state of the HTML document being displayed.

An applet is treated as a self-contained object by a Web browser and is handled in much the same way as an inline image. On the other hand, scripts are integrated within the document's HTML. Because the browser executes the script and not the Java runtime interpreter, it has the capability to modify the contents of the entire browser window, a task that an applet cannot perform.

Applets and scripts each serve a different purpose. Scripts provide the capability to dynamically alter the content of an HTML document, whereas applets are separate, interactive applications that execute within the context of the browser and display information in a limited area of the browser window.

The Execution of an Applet

When the browser comes across the applet tag, it begins gathering the information needed to launch the applet. After the HTML document has been completely interpreted and displayed, the Java runtime interpreter is requested to execute the applet.

When the interpreter receives the request to execute the applet, it executes a loader mechanism to fetch the binary file. After the file is successfully transferred onto the local machine, it undergoes a number of tests to verify its security and stability. Chapter 39, "Java Security," covers applet security. If all is well, the interpreter begins execution of the applet.

Execution continues until the applet terminates or the current browser document is dismissed. This can occur in a couple of different ways: The user might jump to another URL or the browser might terminate. In either case, the applet is terminated.

Using Window Components

Window components can be added to the applet to create a friendly user interface. Java provides a number of window components, which can be found in the java.awt library.

All of the window components added to an applet are confined to the applet window area in the current browser document. The applet window contains the entire paintable region; anything that does not fit inside that region is clipped.

Using the provided window components, applets have the capability to contain the same look and feel of other window application programs. Applets can use all GUI components that can be used by Panel objects. In addition, applets have access to many of the capabilities provided by the browser.

Adding Content and Protocol Handlers to Applets

Java supplies a set of interfaces that support the development of content and protocol handlers. Chapters 28, "Content Handlers," and 29, "Protocol Handlers," cover the use of these interfaces. Content and protocol handlers may be added to applets to provide the capability to retrieve and display different types of Web objects or to use services provided by new or custom Internet protocols. By using these content and protocol handlers, Java applications become more capable network clients and can be used to support distributed processing applications, as discussed in Chapter 32.

Using Applets as Inline Viewers

Applets have the capability to present images, sounds, and other multimedia objects inside an HTML document. Scripts provide the means to dynamically change the content of an HTML document. The combination of the two produces very powerful results, including the capability to use applets as inline viewers.

The interactive Web catalog is a perfect example of the inline viewer concept. A Web page contains a script-enabled order form that contains a selection field. The user uses this selection field to select the product to order. When this happens, the script launches an applet to give a visual presentation of the item selected, providing the user with more information with which to make a purchase decision.

Using an applet as an inline viewer allows for more flexibility than standard HTML facilities. The content to be viewed might contain contextual interactive information rather than being a flat piece of media.

Using Applets as Network Clients

The concept of containing a network client inside an HTML document might seem a little strange to you, and for good reason-embedding a client inside a client seems redundant. However, the browser client can be viewed as a delivery mechanism for the embedded client, allowing Web-based support of other application protocols.

By combining applets with protocol handlers, an applet can quickly become a Telnet, an FTP, or even an embedded HTTP client. If a custom protocol handler is used, the applet can become the means for users to access custom network applications.

Optimizing Applets

Like most applications on the Internet, applets are constrained by their user's communication bandwidth capabilities. You know it is a good idea to optimize Web pages for optimal low-bandwidth performance; the same holds true for applets.

Fortunately, graphics usually constitute a much greater physical size than the applet's executable size. I say "fortunately" because it is far easier to optimize the loading of media than to worry about code generation.

Applets, being a mechanism for a graphical user interface, usually require the transfer of graphics files. This process can take varying amounts of time depending on the speed of the connection. Taking the approach of loading media in the background, while the user is busy doing something else, limits the annoying delays associated with media transfers.

Hopefully, low bandwidth will not always be a problem for Internet applications. However, for as long as users have to wait for something to display on their screen, optimizing applications for low-bandwidth situations will produce applications that are more likely to satisfy user expectations.

Learning from Online Examples

As technologies advance, the early adopter usually provides examples to promote the technology. So is the case with scripts and applets. A number of companies have jumped on the Java wagon from the get-go and provide examples of their endeavors. The following are a few sites where valuable information can be found:

Summary

This chapter gives background information on how applets work and how they relate to scripts written in the JavaScript language. You have also been given a little taste of what applets and scripts can achieve. Chapter 32 discusses how applets can be integrated into distributed processing applications. This chapter is designed to get your proverbial wheels turning.