The APPLET tag

You can insert Java applets into your HTML pages by using the APPLET tag. In its simplest form, the APPLET tag looks like this:

<APPLET CODE=classfile.class WIDTH=50 HEIGHT=50></APPLET>

Like the IMG tag, the APPLET tag has a WIDTH field and a HEIGHT field. Applets often require a specific WIDTH or HEIGHT, often the width or height of the background image the applet uses. Also like the IMG tag, the APPLET tag has a set of optional fields for positioning the applet on the page:

ALIGN - vertical or horizontal alignment
HSPACE - horizontal margins
VSPACE - vertical margins

The text between the <APPLET…> and </APPLET> is seen only by people whose browser doesn't support Java. It's helpful to add a little note for those people, for example:

<APPLET CODE=classfile.class WIDTH=50 HEIGHT=50>
Your browser doesn't support Java
</APPLET>

Most Java applets can be customized for your page. You pass custom information to the applet through the PARAM tag.

<PARAM NAME="Background" VALUE="bkgnd.gif">
<PARAM NAME="speed" VALUE="200">

Every PARAM tag has two fields: NAME and VALUE. The NAME field is the name, in double-quotes, of the parameter you wish to customize. The VALUE field is the new value, also in double-quotes, that you want that parameter to have.

NOTE: Always remember to put the double-quotes around PARAM name's and values - it's very easy to forget them.
NOTE: always type the parameter name exactly - it's easy to accidentally type lower-case instead of upper-case or to forget an initial capital.

Putting this all together, here's a simple APPLET tag with parameters:

<APPLET CODE=blinky.class WIDTH=150 HEIGHT=30>
<PARAM NAME="Rate" VALUE="27">
<PARAM NAME="Text" VALUE="Hello from Java!">
Your browser doesn't support Java.
</APPLET>

Where does the applet come from?

I've just shown you how to modify your web page to add an applet, but where does the applet come from?

Applets, like images, can be stored anywhere on your web server. The simplest form (the one shown above) requires that the applet's .class file be stored in the same directory as the web page that uses it. In the above example, of the applet named 'blinky.class', you would have to copy the java file 'blinky.class' to the directory your web page is in.

To store your applet somewhere other than the same directory as your web page, add the CODEBASE field to the APPLET tag:

<APPLET CODEBASE=things CODE=blinky.class WIDTH=150 HEIGHT=30>

In this example, the file 'blinky.class' is in a directory called 'things' in the same directory as your web page. You can use the CODEBASE field to refer to .class files anywhere, for example '/allmyjava/blinking' or '../applets'.