The Applet Tag


We have already seen many examples of the applet tag. It is, of course, used to place an applet at an indicated spot in a web page. As far as we know, it looks as follows:
<APPLET CODE="ClassName.class"
        WIDTH=nnn
        HEIGHT=mmm>
</APPLET>
Actually, there is more to the story. A more complete applet tag can also include the CODEBASE option, as follows:
<APPLET CODEBASE="http://machine.name/directory"
        CODE="ClassName.class"
        WIDTH=nnn
        HEIGHT=mmm>
</APPLET>
which allows you to load an applet into your web page even if that applet is located on another machine on the internet. In many cases, the applet and the corresponding HTML page are in the same directory, in which case the CODEBASE option is not needed. If, however, your applet is in another directory than the enclosing HTML page, or even on another machine, you must provide the CODEBASE tag to refer to the applet's location.

That also clarifies the difference between the Applet methods getDocumentBase() and getCodeBase():
getDocumentBase() returns the base location of the HTML document that contains the applet
getCodeBase() returns the base location of where the applet is located, as specified by the CODEBASE option.
For most applet this distinction may not be necessary. However, a further enhancement of the APPLET tag is very useful to create applets that can be used in different web pages and are configuarable from within the web page: the PARAM tag inside the APPLET tag. For example, say we want to create an applet that contains a list of items, but you need to determine the items inside the web page and pass them into the applet. Your improved APPLET tag might look like this:
<APPLET CODE="ParamTest.class" WIDTH=200 HEIGHT=200>
   <PARAM NAME="item1" VALUE="Bert Wachsmuth">
   <PARAM NAME="aName" VALUE="Joe Smith">
   <PARAM NAME="another" VALUE="Jane Doe">
</APPLET>
Inside the applet, the method String getParameter(String name) will return the value for the PARAM tag with the given name. For example, here is a code to read the above values from the PARAM tags:
import java.applet.*;
import java.awt.*;

public class ParamTest extends Applet
{
   private List list = new List();

   private void init()
   {
      list.addItem(getParameter("item1"));
      list.addItem(getParameter("aName"));
      list.addItem(getParameter("another"));
      setLayout(new BorderLayout());
      add("Center", list);
   }
}
When you compile this applet and load it via the above APPLET tag, the applet will read the values from the named parameters inside the HTML page, and display them in a list.

PARAM tags are very useful for "real" applet that can be configured as you embed them into a web page. You can change the applet's behaviour by simply adjusting the value of a named parameter; you do not have to recompile the applet. For another example, please check the nexture on "Loading Images".


(bgw)