Embedding Applets in Web Pages

HyperText Markup Language, or HTML, is the language of web pages. A basic HTML page might display only text when loaded in a web browser. However, unlike normal ASCII text, HTML lets you markup text using various embedded tags that are invisible to a person viewing the page via a web browser. These tags let you author a web page that has most of the features of a word processor document.

One of the most common types of markup is the inclusion of images. For instance, consider this graphic: . The actual HTML code for embedding the image looks like

<IMG SRC="../images/smiley.gif" WIDTH=17 HEIGHT=17>

The text starting with the '<' character and ending with the '>' character is an HTML tag. The IMG token tells the web browser that this is an image tag; and the SRC="../images/smiley.gif" text is an attribute that tells where to get the graphic file. Similarly, the WIDTH and HEIGHT attributes tell how large to scale the image. That looks simple enough, right? Applets are embedded in HTML much the same way.

As was described in Applets & Web Browsers, an applet is a program that runs inside an HTML page that is loaded in a web browser. While an applet may offer features that are nearly as complex as the web browser in which it is running, the task of embedding the applet in a web page is quite simple. The basic skeleton of an applet tag looks like:

<APPLET ARCHIVE="sitesurf.jar" CODE=SiteSurfer.class WIDTH=140 HEIGHT=45>
<PARAM NAME="ANAME" VALUE="AVALUE">
<PARAM NAME="BNAME" VALUE="BVALUE">
...
...
</APPLET>

First, there is the APPLET element, followed by 4 attributes. We have already seen the WIDTH and HEIGHT elements; the other two are new. The ARCHIVE element specifies the archive file that has stores the executable code for the applet. Before the web browser starts the applet, it first downloads this file. Next, the web browser looks for the class file specified by the CODE attribute. In the above example, there should be a file called SiteSurfer.class archived in the sitesurf.jar file. If not, and if the file does not exist anywhere else that the web browser can determine, then the applet will fail to load.

The APPLET element also allows a few other attributes. The CODEBASE attribute is used to tell the web browser where to find the files specified in the ARCHIVE and CODE attributes. Normally, a web browser will try to load necessary files from the same directory as the HTML page. The value of the CODEBASE attribute is a URL that specifies where to look; it may be an absolute link or relative to the "document base." For example, consider

<APPLET CODEBASE="../classes/" ARCHIVE="sitesurf.jar" CODE=SiteSurfer.class WIDTH=140 HEIGHT=45>

Here the web browser has been instructed to load the sitesurf.jar file by going up one directory and then down into the classes subdirectory. More precisely, if the HTML page were located at http://www.mycorp.com/pages/index.htm, then there should also be a file at http://www.mycorp.com/classes/sitesurf.jar.

Every embedded applet using the <APPLET> tag must be closed with a </APPLET> tag. In between may be placed any number of statements that look like

<PARAM NAME="ANAME" VALUE="AVALUE">

Each such line constitutes an applet parameter. A parameter is usually used to pass setup information to the applet. Each applet parameter requires both a NAME and a VALUE attribute. For instance, one of SiteSurfer's applet parameters is named HELPURL, which tells the SiteSurfer applet where to find the help page when a user presses the help button. In action it looks like

<PARAM NAME="HELPURL" VALUE="help.htm">

Another common parameter is named CABBASE. Unlike most other parameters, it is not used to send information to the applet. Rather, it is a hint to Microsoft Internet Explorer that tells it the name of a special archive file, known as a Cab archive, that has the code for the applet. Why do you need both CABBASE and ARCHIVE you ask? Well, the files specified by the ARCHIVE attribute must be in Zip or Jar format, but Cab files use a proprietary format the only works with Internet Explorer. On the plus side, it is usually smaller than Zip or Jar archives containing the same data. It looks like this:

<PARAM NAME="CABBASE" VALUE="sitesurf.cab">

When all of these element are put together, we arrive at a well-formed, fully-functional applet expression:

<APPLET CODEBASE="../classes/" ARCHIVE="sitesurf.jar" CODE=SiteSurfer.class WIDTH=140 HEIGHT=45>
<PARAM NAME="CABBASE" VALUE="sitesurf.cab">
<PARAM NAME="HELPURL" VALUE="help.htm">
</APPLET>