To add an applet to an HTML page, use the APPLET tag. This page provides a
couple of examples, then defines the complete syntax of the APPLET tag,
including a description of each of the tag's attributes.
A Simple Example
<applet code="MyApplet.class" width=100 height=140></applet>
This example tells the 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.
A More Complex Example
<applet codebase="http://java.sun.com/applets/1.1/NervousText"
code="NervousText.class" width=400 height=75>
<param name="text" value="HotJava!">
<hr>
If you were using HotJava, you would see dancing text instead
of this paragraph.
<hr>
</applet>
This tells the browser to load the applet whose compiled
code is at the URL
http://java.sun.com/applets/NervousText/1.1/NervousText.class,
and to set the initial size of the applet to 400 pixels wide by 75 pixels high.
The browser must also set the applet's "text" parameter (which specifies the
text this applet displays) to be "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 a bit for the applet to be
loaded.)
The APPLET Tag Syntax
Required elements are in bold. Optional elements are in regular
typeface. Elements you specify are in italics.
<APPLET
CODEBASE = codebaseURL
CODE = appletFile ...or... OBJECT = serializedApplet
WIDTH = pixels HEIGHT = pixels
NAME = appletInstanceName
ALIGN = alignment
VSPACE = pixels HSPACE = pixels
ARCHIVE = archiveList
ALT = alternateText
>
<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 (or OBJECT), WIDTH, and HEIGHT. Each attribute is described
below.
-
CODEBASE = codebaseURL
-
This optional attribute specifies the base URL of the applet.
This is where the browser looks to find the .class file defined
by the CODE attribute. If you don't specify CODEBASE, the browser
looks for the .class file in the same directory as the HTML file
containing the applet tag.
-
CODE = appletFile
-
This required attribute is the name of the file that contains
the compiled applet. This is always a file with a .class extension.
For example, to include an applet called MyApplet in an HTML page,
you would specify:
CODE="MyApplet.class"
-
OBJECT = serializedApplet
-
This attribute is an alternative to the CODE attribute. (Either CODE
should be used, or OBJECT, but not both.) OBJECT gives the name of
a file that contains a serialized applet. This is a "snapshot"
of an applet that has been previously started and stopped. When the
applet is serialized, its current state is captured to be restarted
at a later time. For example, a game applet might be serialized and
restarted at a later time from the point in the game where the applet
was stopped.
NOTE: The attributes valid when the applet was previously started,
such as WIDTH, HEIGHT, and the PARAM attributes, are not saved when the
applet is serialized. The attributes passed to this instance of the applet
may be different than the original attributes, which could potentially
cause unexpected behavior.
-
WIDTH = pixels HEIGHT = pixels
-
These required attributes give the width and height (in pixels) of
the applet display area, not counting any windows or dialogs that
the applet brings up.
-
NAME = appletInstanceName
-
This optional attribute specifies a unique name for this instance of
the applet, which makes it possible for applets on the same page to
find (and communicate with) each other.
-
ALIGN = alignment
-
This optional attribute specifies the alignment of the applet with the
text that surrounds it. If you don't specify this attribute, the bottom
of the applet is typically aligned with the bottom of the adjacent
text, but this can vary among browsers.
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 option 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.
-
ARCHIVE = archiveList
-
This optional attribute is a list of one or more JAR (Java Archive) files
containing resources the applet will reference, such as classes (java code)
and images. The advantage of using archives is to minimize the number of
HTTP accesses to obtain resources. The archive file is preloaded before
the applet is run; then when the applet needs a resource, it searches the
preloaded archives first.
If you have more than one archive files, separate each file with a ,
(comma).
-
ALT = alternateText
-
This optional attribute specifies any text that should be displayed
if the browser can generally run Java applets, but it can't display
this particular applet (for example, if this applet can't be loaded).
-
<PARAM NAME = appletAttribute1 VALUE = value>
<PARAM NAME = appletAttribute2 VALUE = value> . . .
-
Use this tag to specify applet-specific attributes. The applet writer
specifies what (if any) parameters the applet accepts or requires.
Some applets have no parameters, while others might require that you
specify several.
-
alternateHTML
-
This optional HTML at the end of the applet tag specifies any
text that should be displayed if the browser understands the
APPLET tag but can't run Java applets. In the example at the
top of this page, the alternateHTML was the HTML between, and
including, the <hr> tags.
When the user-specified attributes are text, such as codebaseURL,
it is generally a good idea to enclose the text in double quotes, as
was done in the examples in this document. This will insure that browsers
won't have problems parsing the text.
Back to HotJava Browser Applet Security