The APPLET Element

Applets are embedded in web pages using the <APPLET> and </APPLET> tags. The APPLET element is similar to the IMG element. 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://www.ibiblio.org/javafaq/index.html and that page references an applet with CODE="Animation", then the file Animation.class should be at the URL http://www.ibiblio.org/javafaq/Animation.class.

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 contains 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" 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" 
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.

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.example.com/classes" width="200" height="200">
</applet>

In this case the browser will look for http://www.example.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 | Cafe au Lait

Copyright 1997, 1999, 2002, 2003, 2005 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified July 1, 2005