Adding Applets to Your Web Page

Let's say you've found a nifty Java applet on the World Wide Web and you want to incorporate it into your own Web page. How do you do it?

Use your browser's View Document Source command to take a look at the innards of the page. Locate the <APPLET> tag that defines the applet:

<APPLET CODE=class CODEBASE=url
	WIDTH=n HEIGHT=n
	other-attributes...>
<PARAM NAME=name1 VALUE=value1>
<PARAM NAME=name2 VALUE=value2>
<PARAM ...>
This text is displayed if a browser doesn't support Java.
</APPLET>

(If you can't find this, it's not a Java applet—too bad!) The two most important attributes here are CODE and CODEBASE. The CODE attribute names the applet class file. The extension for a class file is class (in case you want to download the applet). The CODEBASE attribute gives the absolute/relative URL to the file. If CODEBASE isn't present, the applet's URL is the same as its document's URL.

To use the applet, you can just copy the entire block to your page, changing (or adding) CODEBASE as necessary. You might also be able to tweak the applet's appearance or behavior by changing the WIDTH and HEIGHT attributes (which specify the size of the applet), or the VALUEs of any PARAM tags present. (Make sure you don't change the NAMEs of the parameters though). Applet parameters are explained in a bit more detail later, in the context of programming PaintApp.

If you have an applet class file in the same location as your Web page (because you downloaded it or you wrote it yourself), you don't even need the CODEBASE attribute.

Note: Some applets require multiple class files. (You'll know it when the applet doesn't run properly.) If you download one, be sure to download them all!


1