Previous | Next | Trail Map | Writing Applets | Overview of Applets


Adding an Applet to an HTML Page (1.1notes)

Once you've written some code for your applet, you'll want to run your applet to test it. To run an applet in a browser or in the JDK Applet Viewer, the applet needs to be added to an HTML page, using the <APPLET> tag. You then specify the URL of the HTML page to your browser or the Applet Viewer.


Note: Some browsers don't support easy, guaranteed reloading of applets. For this reason, it often makes sense to test applets in the Applet Viewer until the applet reaches a point where you need to test it in a browser.

This page tells you most of what you need to know to use the <APPLET> tag. It starts by showing you the tag's simplest form, and then discusses some of the most common additions to that simple form--the CODEBASE attribute, the <PARAM> tag, and alternate HTML code. For a detailed description of the <APPLET> tag, refer to The <APPLET> Tag.

The Simplest Possible <APPLET> Tag

Here's the simplest form of the <APPLET> tag:
<APPLET CODE=AppletSubclass.class WIDTH=anInt HEIGHT=anInt>
</APPLET>
The above tag tells the browser or applet viewer to load the applet whose Applet subclass, named AppletSubclass, is in a class file in the same directory as the HTML document that contains the tag. The above tag also specifies the width and height in pixels of the applet.

When a browser encounters the tag, it reserves a display area of the specified width and height for the applet, loads the bytecodes for the specified Applet subclass, creates an instance of the subclass, and then calls the instance's init and start methods.

Specifying the Applet Directory with CODEBASE

Here's a slightly more complex applet tag. It adds a CODEBASE attribute to tell the browser/viewer which directory the Applet subclass bytecodes are in.
<APPLET CODE=AppletSubclass.class CODEBASE=aURL
        WIDTH=anInt HEIGHT=anInt>
</APPLET>
By making aURL an absolute URL, you can make a document loaded from your HTTP server run an applet from another HTTP server. If aURL is a relative URL, then it's interpreted relative to the HTML document's location.

This tutorial uses CODEBASE=someDirectory/ frequently, since we put our examples in a subdirectory of the directory that contains the HTML documents. For example, here's the <APPLET> tag that includes the Simple applet in The Life Cycle of an Applet, earlier in this trail:

<APPLET CODE=Simple.class CODEBASE=example/ WIDTH=500 HEIGHT=20>
</APPLET>

Specifying Parameters with the <PARAM> Tag

Some applets let the user customize the applet's configuration with parameters. For example, AppletButton (an applet used throughout this tutorial to provide a button that brings up a window) allows the user to set the button's text by specifying the value of a parameter named BUTTONTEXT. You'll learn how to write the code to provide parameters in Defining and Using Applet Parameters(in the Writing Applets trail).

Here's an example of the format of the <PARAM> tag. Note that <PARAM> tags must appear between the <APPLET> and </APPLET> tags for the applet they affect.

<APPLET CODE=AppletSubclass.class WIDTH=anInt HEIGHT=anInt>
<PARAM NAME=parameter1Name VALUE=aValue>
<PARAM NAME=parameter2Name VALUE=anotherValue>
</APPLET>

Here's an example of the <PARAM> tag in use. It's taken from a page in the UI trail(in the Creating a User Interface trail).

<APPLET CODE=AppletButton.class CODEBASE=example WIDTH=350 HEIGHT=60>
<PARAM NAME=windowType VALUE=BorderWindow>
<PARAM NAME=windowText VALUE="BorderLayout">
<PARAM NAME=buttonText VALUE="Click here to see a BorderLayout in action">
. . .
</APPLET>

Specifying Text to be Displayed by Java-Deficient Browsers

Note the ellipsis (". . .") in the AppletButton HTML example above. What did the example leave out? Alternate HTML code -- HTML code interpreted only by browsers that don't understand the <APPLET> tag.

If the page that contains your applet might be seen by people running non-Java-compatible browsers, you should provide alternate HTML code so that the page still makes sense. Alternate HTML code is any text between <APPLET> and </APPLET> tags, except for <PARAM> tags. Java-compatible browsers ignore alternate HTML code.

We use alternate HTML code throughout the online version of this tutorial to tell readers about the applet they're missing and, if it's helpful, to provide a picture of the applet. Here's the full HTML code for the AppletButton example shown previously:

<APPLET CODE=AppletButton.class CODEBASE=example WIDTH=350 HEIGHT=60>
<PARAM NAME=windowType VALUE=BorderWindow>
<PARAM NAME=windowText VALUE="BorderLayout">
<PARAM NAME=buttonText VALUE="Click here to see a BorderLayout in action">
<HR>
<EM>
Your browser can't run 1.0 Java applets,
so here's a picture of the window the program brings up:</em>
<P>
<IMG SRC=images/BorderEx1.gif WIDTH=302 HEIGHT=138>
<HR>
</APPLET>
An applet that doesn't understand the <APPLET> tag ignores everything until <HR>. An applet that does understand the <APPLET> tag ignores everything between <HR> and </HR>.


Previous | Next | Trail Map | Writing Applets | Overview of Applets