| Documentation Contents

# The APPLET Tag

Here is an example of a simple APPLET tag:

      <applet code="MyApplet.class" width=100 height=140></applet>

This tells the viewer or browser to load the applet whose compiled code is in MyApplet.class (in the same directory as the current HTML document), and to set the initial size of the applet to 100 pixels wide and 140 pixels high.

Here's a more complex example of an APPLET tag:

        <applet codebase="http://java.sun.com/applets/NervousText/1.1"
    	code="NervousText.class" width=400 height=75>
        <param name="text" value="Welcome to HotJava!">
        <hr>
        If you were using a Java-enabled browser such as HotJava,
        you would see dancing text instead of this paragraph.
        <hr>
        </applet>

This tells the viewer or browser to load the applet whose compiled code is at the URL http://java.sun.com/applets/NervousText/1.1/NervousText.class, to set the initial size of the applet to 400x75 pixels. The viewer/browser must also set the applet's "text" attribute (which customizes the text this applet displays) to be "Welcome to HotJava!" If the page is viewed by a browser that can't execute Java applets, then the browser will ignore the APPLET and PARAM tags, displaying only the HTML between the `<param>` and `</applet>` tags (the alternate HTML).

Here's the result of putting the above example in your HTML file. (The first time you load this page, you may have to wait for the applet to be loaded.)

If you were using a Java-enabled browser such as HotJava, you would see dancing text instead of this paragraph.

Here is another example of an APPLET tag:

         <applet code=A21 width=256 height=256 archive="toir.jar">
         <param name=img value=test.gif>
         <hr>
         We need to convert some of the standard applets to use
         archive.  Any volunteers?
         <hr>
         </applet>

In this example, the applet class is A21. Its bytecodes (may) reside in the archive "`toir.jar`". This archive may also contain the image resource (see resources documentation) with name `test.gif`.

Here's the complete syntax for the APPLET tag. Required elements are in bold. Optional elements are in regular typeface. Elements your specify are in italics.

        <APPLET
            CODEBASE = codebaseURL
            ARCHIVE = archiveList
            CODE = appletFile ...or...  OBJECT = serializedApplet
            ALT = alternateText
            NAME = appletInstanceName
            WIDTH = pixels  HEIGHT = pixels
            ALIGN = alignment
            VSPACE = pixels  HSPACE = pixels
        >
        <PARAM NAME = appletAttribute1 VALUE = value>
        <PARAM NAME = appletAttribute2 VALUE = value>
        . . .
        alternateHTML
        </APPLET>

CODE, CODEBASE, and so on are attributes of the applet tag; they give the browser information about the applet. The only mandatory attributes are CODE, WIDTH, and HEIGHT. Each attribute is described below.

CODEBASE = codebaseURL
  This OPTIONAL attribute specifies the base URL of the applet--the directory that contains the applet's code. If this attribute is not specified, then the document's URL is used.
ARCHIVE = archiveList
  This OPTIONAL attribute describes one or more archives containing classes and other resources that will be "preloaded". The classes are loaded using an instance of an AppletClassLoader with the given CODEBASE.
  The archives in archiveList are separated by ",". NB: in JDK1.1, multiple APPLET tags with the same CODEBASE share the same instance of a ClassLoader. This is used by some client code to implement inter-applet communication. Future JDKs may provide other mechanisms for inter-applet communication. For security reasons, the applet's class loader can read only from the same codebase from which the applet was started. This means that archives in archiveList must be in the same directory as, or in a subdirectory of, the codebase. Entries in archiveList of the form `../a/b.jar` will not work unless explicitly allowed for in the security policy file (except in the case of an http codebase, where archives in archiveList must be from the same host as the codebase, but can have ".."'s in their paths.)
CODE = appletFile
  This REQUIRED attribute gives the name of the file that contains the applet's compiled Applet subclass. This file is relative to the base URL of the applet. It cannot be absolute. One of CODE or OBJECT must be present. The value appletFile can be of the form classname`.class` or of the form packagename`.`classname`.class`.
OBJECT = serializedApplet
  This attribute gives the name of the file that contains a serialized representation of an Applet. The Applet will be deserialized. The init() method will *not* be invoked; but its start() method will. Attributes valid when the original object was serialized are *not* restored. Any attributes passed to this APPLET instance will be available to the Applet; we advocate very strong restraint in using this feature. An applet should be stopped before it is serialized. One of CODE or OBJECT must be present.
ALT = alternateText
  This OPTIONAL attribute specifies any text that should be displayed if the browser understands the APPLET tag but can't run Java applets.
NAME = appletInstanceName
  This OPTIONAL attribute specifies a name for the applet instance, which makes it possible for applets on the same page to find (and communicate with) each other.
WIDTH = pixels HEIGHT = pixels
  These REQUIRED attributes give the initial width and height (in pixels) of the applet display area, not counting any windows or dialogs that the applet brings up.
ALIGN = alignment
  This OPTIONAL attribute specifies the alignment of the applet. The possible values of this attribute are the same as those for the IMG tag: left, right, top, texttop, middle, absmiddle, baseline, bottom, absbottom.
VSPACE = pixels HSPACE = pixels
  These OPTIONAL attributes specify the number of pixels above and below the applet (VSPACE) and on each side of the applet (HSPACE). They're treated the same way as the IMG tag's VSPACE and HSPACE attributes.
<PARAM NAME = appletAttribute1 VALUE = value>
<PARAM NAME = appletAttribute2 VALUE = value> . . .
  This tag is the only way to specify an applet-specific attribute. Applets access their attributes with the getParameter() method.

Copyright © 1995-2010 Sun Microsystems, Inc. All Rights Reserved. Please send comments using this Feedback page. | Java Technology