From the viewpoint of an applet, the interaction involves defining a
subclass of the java.applet.Applet class. The
Applet class defines a number of methods that
control the applet. A subclass of Applet overrides
one or more of the methods:
From the viewpoint of the host application, the interaction
typically follows a standard sequence of events. The host application
usually does the following:
Web pages are written in a language called HTML. This explanation of
how to embed an applet in a Web page assumes that you have some
knowledge of basic HTML. An applet is embedded in a Web page using
an <applet> tag. A minimal
<applet> tag looks as follows:
<applet code=Clock height=300 width=350>
The code attribute of this sample
<applet> tag specifies that the applet
to be run is a class named Clock. The
width and height
attributes specify that the applet should be given a screen area
that is 300 pixels high and 350 pixels wide.
The following list shows all of the attributes that can be specified
in an <applet>
tag. The attributes should be specified in the order in which they
are listed. The code, height,
and width attributes are required in an
<applet> tag; the other attributes are optional:
The codebase attribute should
specify a URL that identifies the directory used to find the
files needed for the applet. Files for classes that belong to the
default package should be in this directory. Files for classes that
belong to named packages should be in subdirectories of this directory,
where the relative path is specified by individual identifiers in
the package name. If codebase is not specified,
the <applet> tag uses the directory that
contains the HTML file as a default.
The code attribute specifies
the name of the class that implements the applet. If the applet
is part of a named package, you must specify the fully qualified
class name. So, if the name of the class is
and it is part of a package called COM.geomaker.graph,
the value of the code attribute should be:
The browser locates the compiled code for the class
by appending .class to the filename and searching
the directory specified by the base URL for the document.
The object attribute specifies the name of a file
that contains a serialized representation of an applet. If this
attribute is specified, the applet is created by deserialization,
rather than by calling its default constructor. The serialization is
assumed to have occurred after the applet's init()
method has been invoked, so the start() method is called
instead of the init() method. Any attributes
specified when the applet was serialized are not restored; the applet
sees the attributes specified for this invocation.
The object attribute is new as of Java 1.1. An
<applet> tag must include either the
code attribute or the object
attribute, but it cannot include both.
The archive attribute specifies a list of one or
more archives that contain classes or other resources for an
applet. Archives can be JAR or ZIP files. If this attribute is
specified, the resources in the archives are loaded before the applet
If multiple archives are listed, they should be separated by
commas. The archive attribute is new for Java
The alt attribute specifies the
text that should be displayed by Web browsers that understand the
<applet> tag but cannot run Java applets.
If the text contains space characters, it should be enclosed in
The name attribute specifies
a name for a particular instance of an applet. An applet can get a
reference to another applet on the same Web page using the
The width attribute specifies
the width of the applet in pixels.
The height attribute specifies
the height of the applet in pixels.
The align attribute specifies
the positioning of the applet. The possible values are:
right, top, texttop,
baseline, bottom, or
The vspace attribute specifies
the amount of vertical space above and below the applet in pixels.
The hspace attribute specifies
the amount of horizontal space to the left and right of the applet
parameters can be provided to an applet using <param>
tags inside the <applet> tag.
A <param> tag must specify
name and value attributes. For example:
<param name=speed value=65>
If a Web browser does not support the <applet>
tag, it ignores the tag and simply displays any HTML content provided
inside the tag. However, if the browser understands the
tag, this HTML content is ignored. This means that you can provide
HTML content inside an <applet> tag to inform
users of non-Java-enabled browsers about what they are missing.
Here is an example that combines all of these elements:
<applet code=Compass height=400 width=300>
<param name=direction value=north>
<param name=speed value=65>
<i>If you can see this message, your Web browser is not Java enabled.
There is a Java applet on this Web page that you are not seeing.</i>
If a non-Java-enabled browser is used to view this HTML file, the following
text is displayed:
If you can see this message, your Web browser
is not Java-enabled. There is a Java applet on this Web page that
you are not seeing.