The APPLET HTML Tag

Applets are embedded in web pages using the <APPLET> and </APPLET> tags. The <APPLET> tag is similar to the <IMG> tag. Like <IMG> <APPLET> references a source file that is not part of the HTML page on which it is embedded. IMG elements do this with the SRC attribute. APPLET elements do this with the CODE attribute. The CODE attribute tells the browser where to look for the compiled .class file. It is relative to the location of the source document. Thus if you're browsing http://metalab.unc.edu/javafaq/index.html and that page references an applet with CODE="Animation.class", then the file Animation.class should be at the URL http://metalab.unc.edu/javafaq/animation.class.

For reasons that remain a mystery to HTML authors everywhere if the applet resides somewhere other than the same directory as the page it lives on, you don't just give a URL to its location. Rather you point at the CODEBASE. The CODEBASE attribute is a URL that points at the directory where the .class file is. The CODE attribute is the name of the .class file itself. For instance if on the HTML page of the previous section you had written

<APPLET CODE="HelloWorldApplet.class" CODEBASE="classes" 
WIDTH=200 HEIGHT=200>
</APPLET>
then the browser would have tried to find HelloWorldApplet.class in the classes directory in the same directory as the HTML page that included the applet. On the other hand if you had written

<APPLET CODE="HelloWorldApplet.class" 
CODEBASE="http://www.foo.bar.com/classes" WIDTH=200 HEIGHT=200>
</APPLET>
then the browser would try to retrieve the applet from http://www.foo.bar.com/classes/HelloWorldApplet.class regardless of where the HTML page was.

In short the applet viewer will try to retrieve the applet from the URL given by the formula (CODEBASE + "/" + code). Once this URL is formed all the usual rules about relative and absolute URLs apply.

You can leave off the .class extension and just use the class name in the CODE attribute. For example,

<APPLET CODE="HelloWorldApplet" 
CODEBASE="http://www.foo.bar.com/classes" WIDTH=200 HEIGHT=200>
</APPLET>
If the applet is in a non-default package, then the full package qualified name must be used. For example,

<APPLET CODE="com.macfaq.greeting.HelloWorldApplet" 
CODEBASE="http://www.foo.bar.com/classes" WIDTH=200 HEIGHT=200>
</APPLET>
In this case the browser will look for http://www.foo.bar.com/classes/com/macfaq/greeting/HelloWorldApplet.class so the directory structure on the server should also mirror the package hierarchy.

The HEIGHT and WIDTH attributes work exactly as they do with IMG, specifying how big a rectangle the browser should set aside for the applet. These numbers are specified in pixels and are required.


Previous | Next | Top
Last Modified June 25, 1999
Copyright 1997, 1999 Elliotte Rusty Harold
elharo@metalab.unc.edu