applet
, object
and
embed
TagsJava Rich Internet Applications Guide > Applet Developer's Guide > Using applet
, object
, and embed
Tags
This sections explains how to add Java applets to Web pages with the
applet
, object
, and embed
tag. In addition, this section provides guidelines for deploying applets on the Internet and on intranets, and for use with different browsers.
The following topics are covered:
applet
tagobject
tagembed
tagHow you deploy an applet depends on whether users access the Web page through the Internet or an Intranet, and the type of browser they use. Note this information about your users, then follow the general guidelines below.
When deploying applets:
Use the applet
tag if the Web page is accessed through
the Internet.
object
or
embed
tag if the Web page is
accessed through an intranet.When deploying applets:
For Internet Explorer only, use the
object
tag.
For the Mozilla family of browsers
only, use the embed
tag.
If you must deploy an applet in a mixed-browser environment, follow the guidelines in the section Deploying Applets in a Mixed-Browser Environment.
applet
tagYou use the applet tag to deploy applets to a multi-browser environment.
For complete details on the applet
tag, see the following:
Note: The HTML specification states that the applet
tag is deprecated, and that you should use the object
tag instead. However, the specification is vague about how browsers
should implement the object
tag to support Java
applets, and browser support is currently inconsistent. Oracle
therefore recommends that you continue to use the
applet
tag as a consistent way to deploy Java applets
across browsers on all platforms.
The following is the syntax for the applet
tag. Required attributes are in bold. Optional attributes are in regular
typeface. Values you 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 legacy_lifecycle = boolean > <param name = appletAttribute1 value = value1> <param name = appletAttribute2 value = value2> ... 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
archive = archiveList
AppletClassLoader
with the given codebase
.archiveList
are separated by commas (,
) Note: in
JDK 1.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 the symbol for parent directory (..
) in their paths.)code = appletFile
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
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
applet
tag but can't run
Java applets.name = appletInstanceName
width = pixels
height =
pixels
align = alignment
img
tag: left
, right
, top
, texttop
, middle
, absmiddle
, baseline
,
bottom
, and absbottom
.vspace = pixels
hspace = pixels
vspace
) and on each side of the applet
(hspace
). They're treated the same way as the img
tag's vspace
and
hspace
attributes.legacy_lifecycle = boolean
true
, a running applet is not destroyed when the user navigates away from a page. Furthermore, when the user navigates back to the page, the running applet is resumed unless the following occurs:
<param name = appletAttribute1 value =
value1>
<param name = appletAttribute2 value = value2> ...
getParameter()
method.object
tagYou use the object
tag to deploy applets that are
to be used only with Internet Explorer. For complete details on the
object
tag, read the W3 HTML
specification. Following is an example of the
object
tag:
<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="200" height="200"> <PARAM name="code" value="Applet1.class"> </OBJECT>
The classid
attribute identifies which minimum
version of Java Plug-in to use.
The example shown below is the most commonly used form of the
classid
attribute. This example instructs Internet
Explorer to use the highest installed version of Java Plug-in.
classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
Following is an alternative form of the classid
attribute:
classid="clsid:CAFEEFAC-xxxx-yyyy-zzzz-ABCDEFFEDCBA"
In this form, "xxxx", "yyyy", and
"zzzz" are four-digit numbers that identify the minimum
version of Java Plug-in to be used. For example, to use Java
Plug-in version 1.8
, you specify:
classid="clsid:CAFEEFAC-0018-0000-0000-ABCDEFFEDCBA"
See Deploying Java Applets With Family JRE Versions in Java Plug-in for Internet Explorer for more information about the classid
attribute.
You use the optional codebase
attribute to specify
if and how to download the JRE. With the codebase=<URL>
form, if the JRE specified by the classid
attribute is not installed locally, then the user is prompted to
download the JRE from the URL specified.
embed
tagYou use the embed
tag to deploy applets that are to
be used only with the Mozilla family of browsers.
Following is an example of the embed
tag:
<embed code="Applet1.class" width="200" height="200" type="application/x-java-applet;version=1.8" pluginspage="http://www.oracle.com/technetwork/java/javase/downloads/ea-jsp-142245.html"/>
The type attribute can have one of two forms:
type="application/x-java-applet;version=1.8"
application/x-java-applet;version=1.8
is
invoked to run the applet. If a JRE with a version number equal to
or greater than the version number specified is installed locally,
then that JRE is invoked. Otherwise the user is directed to the URL
specified as the value of the pluginspage
attribute.type="application/x-java-applet;jpi-version=1.8.0_181
jpi-version
(in this example,
1.8.0_181) is invoked to run the applet. Otherwise the user is
directed to the URL specified as the value of the
pluginspage
attribute.You can deploy applets for users of both Internet Explorer and the Mozilla family of browsers in one of two ways:
When using a pure HTML approach to deploy applets in a mixed-browser environment, note the following:
Internet Explorer
object
tagcomment
tagobject
tag with the classid
attributecomment
tagConsider the following example code from an HTML page:
<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" <param name="code" value="Applet1.class"> <comment> <embed code="Applet1.class" type="application/x-java-applet;version=1.8"> <noembed> No Java Support. </noembed> </embed> </comment> </object>
Instead of using the pure HTML approach described above, you can use JavaScript to deploy applets in a mixed-browser environment.
Through JavaScript, you:
Detect the user's browser through the
appName
variable.
Use the document.write()
method to write a tag
based on the value of the appName
variable:
If the browser name equals "Mozilla", write the
embed
tag.
If the browser name equals "Microsoft Internet
Explorer", write the object
tag.
In the following example, the document.write()
method outputs either an embed
or object
tag for each user “on the fly”:
<html> <script language="Javascript"> var _app = navigator.appName; if (_app == 'Mozilla') { document.write('<embed code="Applet1.class"', 'width="200"', 'height="200"', 'type="application/x-java-applet;version=1.8.0">'); } else if (_app == 'Microsoft Internet Explorer') { document.write('<OBJECT ', 'classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"', 'width="200"', 'height="200">', '<PARAM name="code" value="Applet1.class">', '</OBJECT>'); } else { document.write('<p>Sorry, unsupported browser.</p>'); } </script> </html>