Using applet
, object
and embed
Tags
Java 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:
- General Considerations
- Using the
applet
tag - Using the
object
tag - Using the
embed
tag - Deploying Applets in a Mixed-Browser Environment
General Considerations
How 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.
Deploying Applets on the Internet Versus an Intranet
When deploying applets:
Use the
applet
tag if the Web page is accessed through the Internet.- Use the
object
orembed
tag if the Web page is accessed through an intranet.
Deploying Applets for Specific Browsers
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.
Using the applet
tag
You 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_lifestyle = 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
- 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
AppletClassLoader
with the givencodebase
.
The archives inarchiveList
are separated by commas (,
) Note: in JDK 1.1, multipleapplet
tags with the samecodebase
share the same instance of aClassLoader
. 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
-
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 ofcode
orobject
must be present. The valueappletFile
can be of the formclassname.class
or of the formpackagename.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 itsstart()
method will. Attributes valid when the original object was serialized are not restored. Any attributes passed to thisapplet
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 ofcode
orobject
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
, andabsbottom
. 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 theimg
tag'svspace
andhspace
attributes. legacy_lifecycle = boolean
-
If this optional attribute is set to
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:- The browser reissues the request for the applet definition.
- The response to this request produces an applet definition that differs from the applet definition that was returned by the original request.
<param name = appletAttribute1 value = value1>
<param name = appletAttribute2 value = value2> ...-
This tag is the only way to specify an applet-specific attribute. Applets access their attributes with the
getParameter()
method.
Using the object
tag
You 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
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.6
, you specify:
classid="clsid:CAFEEFAC-0016-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.
The codebase Attribute
You use the optional codebase
attribute to specify if and how to download the JRE.
The codebase
attribute has two forms:
codebase=<URL>
With this 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.
codebase=<URL>#Version=major,minor,micro,update
With this form, if the version of the JRE specified in the codebase attribute is greater than the version of the JRE specified by the
classid
attribute, then the user is prompted to download the JRE from the URL specified.
Following is an example of how to use the codebase
attribute to set up automatic downloads from the Sun Java Web site:
<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="200" height="200" codebase="http://java.sun.com/products/plugin/autodl/
jinstall-1_5_0-windows-i586.cab#Version=1,5,0,0"> <param name="code" value="Applet1.class"> </object>
Note: In this example the codebase=http://java.sun.com ...
line is broken for readability. In the actual HTML file it would be one long line.
Oracle has packaged each version of the JRE installer in Microsoft cabinet (.cab
) file format. See Autodownload Files for a list of these releases and the corresponding .cab
file names.
Using the embed
tag
You 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.6" pluginspage="http://java.sun.com/javase/downloads/ea.jsp"/>
The type attribute can have one of two forms:
type="application/x-java-applet;version=1.6"
With this form, the highest installed JRE that supports the MIME type i.e.application/x-java-applet;version=1.6
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 thepluginspage
attribute.
type="application/x-java-applet;jpi-version=1.5.0_07
With this form, a JRE with at least the specified update version given by the value ofjpi-version
(in this example, 1.5.0_07) is invoked to run the applet. Otherwise the user is directed to the URL specified as the value of thepluginspage
attribute.
Deploying Applets in a Mixed-Browser Environment
You can deploy applets for users of both Internet Explorer and the Mozilla family of browsers in one of two ways:
Deploying Applets Using Pure HTML
When using a pure HTML approach to deploy applets in a mixed-browser environment, note the following:
Internet Explorer
- Recognizes the
object
tag - Ignores the contents of the
comment
tag
- Recognizes the
- Mozilla browsers
- Ignore an
object
tag with theclassid
attribute - Interpret the contents of the
comment
tag
- Ignore an
Consider 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.6"> <noembed> No Java Support. </noembed> </embed> </comment> </object>
Deploying Applets Using JavaScript
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 theappName
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.5.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>