Graphic by Keith Ohlfs |
from HTML |
Applets are black-box programs that can be executed within a web page by a Java-enabled browser. This document explains how to invoke applets from HTML. It begins with a discussion of applet tags and parameters, and then explains how to invoke a remote applet from your local web pages.
An applet invocation is specified using the pair of tags <applet> and </applet>. The <applet> tag has four attributes:
The code, width, and height attributes are all required. The optional codebase attribute is perhaps the trickiest aspect of calling an applet and deserves special attention. The value of the codebase attribute is always the name of a directory, and never the name of a file. So a codebase attribute never ends in an extension (e.g., .html, .class, .java). The value of the codebase attribute depends on the relationship between the locations of the bytecode file and the HTML file invoking the bytecode file:
<applet codebase="http://www.vivids.com/java/assorted/Coalesce/" code=Coalesce.class width=400 height=200>
<applet code=CS111Applet.class width=400 height=200>
|
|
|
/applets/programs/examples |
/applets |
programs/examples |
/applets/programs |
/applets |
programs |
/applets/programs |
/applets/html |
../programs |
/applets/programs/examples |
/applets/html |
../program/examples |
/applets/programs |
/applets/html/examples |
../../programs |
/applets/programs/examples |
/applets/html/examples |
../../programs/examples |
The behavior of an applet can be controlled by any number of parameters chosen by the implementor of the applet. Each parameter is specified by the following syntax, of which any number of instances (including zero) may appear between the <applet ...> and </applet> tags:
<param name=parameter-name value=parameter-value>
For example, here is the HTML code that invokes a MoveUp.class in the same directory as the HTML file:
<applet code=MoveUp.class width=250 height=50> <param name=TextCount value=2> <param name=text1 value="Twinkle, twinkle"> <param name=text2 value="little star"> <param name=AppBGImage value=../images/pattern.gif> <param name=AppTile value=true> <param name=DelayBetweenChars value=50> <param name=DelayBetweenRuns value=2000> <param name=HorizCenter value=true> <param name=VertCenter value=true> <param name=Font value=Helvetica> <param name=Style value=bold> <param name=Pointsize value=30> </applet>
The names of the parameters are important. For example, one cannot rename text1 to text when there is only one line of text.
The names, possible values, and meanings of each applet parameter are chosen by the creator of the applet. A good applet programmer will associate documentation for this information with each applet. Given just a Java bytecode file, there is no easy way to tell exactly what parameters it handles.
Suppose you find a cool applet on the web that you would like to include in your own set of pages. How can you accomplish this? To understand the issues involved, it is important to realize that an applet usually consists of more resources than just a single .class file:
The failure to correctly handle these additional resources is a common reason why an applet does not work when you attempt to include it in your personal web pages.
There are two basic approaches to including a remote applet in your personal pages:
Each of these approaches has advantages and disadvantages. The first approach requires the overhead of first copying all the applet resource files to your local machine. This goes against the grain of the entire applet notion, which is to dynamically download programs from the web. Leaving applets on the web saves space on your local machine and allows applet users to always get the "latest version" of the applet. On the other hand, repositories for web applets can become temporarily or permanently inaccessible, so it can be prudent to make local copies of applets that you find indispensable and that you fear might disapper from the web.
For the rest of this section, we assume that you are following approach 2: that is, you want to invoke an applet whose .class file and other resources are on a remote machine. We also assume that you can successfully execute the desired applet by following some set of links in your web browser. That is, you found the applet on someone else's web page and want to include it in your own. The following steps explain how to invoke the applet from one of your personal pages:
Here are some examples of extending dir-name with codebase-name. In all cases, assume that dir-name is http://www.cool.com/applets/examples.
|
|
test |
http://www.cool.com/applets/examples/test |
test/code |
http://www.cool.com/applets/examples/test/code |
../test/code |
http://www.cool.com/applets/test/code |
../../test/code |
http://www.cool.com/test/code |
<param name=AppBGImage value=../images/pattern.gif>
Suppose the remote directory containing the HTML file with this tag is named http://www.vivids.com/java/assorted/MoveUp. Then in the local applet invocation template, the above parameter should be changed to
<param name=AppBGImage value="http://www.vivids.com/java/assorted/images/pattern.gif">
Not all web browsers are Java-enabled. A significant number of people surfing the web use text-only browsers (such as Lynx), or older versions of graphical browsers (Netscape, Internet Explorer, Mosaic) that do have support for running applets. Browsers typically ignore tags that they do not implement, so by default these browsers give no indication that there is an applet invocation on a web page that cannot be handled.
It is helpful to give users of Java-challenged browsers an indication that there is an applet on the page that they are not seeing. There are two ways of doing this:
<applet code=CS111Applet.class width=400 height=200 alt="Here is an applet that displays the phrase Welcome to CS111"> </applet>
It is worth noting that most browsers understand a similar alt attribute for the img tag. This is helpful not only for text-only browers, but also for graphical browsers that for some reason cannot find the image.
<applet code=CS111Applet.class width=400 height=200> Because you are running a Java-challenged browser, you are missing out on a wonderful applet that displays the message "Welcome to CS111" in glorious colors. </applet>