This sections explains how to add Java applets to Web pages with the
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:
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.
When deploying applets:
applet tag if the Web page is accessed through
embedtag if the Web page is accessed through an intranet.
When deploying applets:
For Internet Explorer only, use the
For the Mozilla family of browsers
only, use the
If you must deploy an applet in a mixed-browser environment, follow the guidelines in the section Deploying Applets in a Mixed-Browser Environment.
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
tag is deprecated, and that you should use the
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>
codebase, and so on are attributes of the applet tag; they
give the browser information about the applet. The only mandatory
height. Each attribute is described
codebase = codebaseURL
archive = archiveList
AppletClassLoaderwith the given
archiveListare separated by commas (
,) Note: in JDK 1.1, multiple
applettags with the same
codebaseshare 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
Appletsubclass. This file is relative to the base URL of the applet. It cannot be absolute. One of
objectmust be present. The value
appletFilecan be of the form
classname.classor of the form
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
appletinstance 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
objectmust be present.
alt = alternateText
applettag but can't run Java applets.
name = appletInstanceName
width = pixels
height = pixels
align = alignment
vspace = pixels
hspace = pixels
vspace) and on each side of the applet (
hspace). They're treated the same way as the
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> ...
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 classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="200" height="200"> <PARAM name="code" value="Applet1.class"> </OBJECT>
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.
Following is an alternative form of the
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
1.6, you specify:
See Deploying Java Applets With Family JRE Versions in Java Plug-in for Internet Explorer for more information about the
You use the optional
codebase attribute to specify
if and how to download the JRE.
codebase attribute has two forms:
With this form, if the JRE specified by the
attribute is not installed locally, then the user is prompted to
download the JRE from the URL specified.
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
attribute to set up automatic downloads from the Sun Java Web
<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
... 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
.cab) file format. See Autodownload Files for a list of these releases and the corresponding
.cab file names.
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 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:
application/x-java-applet;version=1.6is 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
jpi-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 the
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:
objecttag with the
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>
Detect the user's browser through the
document.write() method to write a tag
based on the value of the
If the browser name equals "Mozilla", write the
If the browser name equals "Microsoft Internet
Explorer", write the
In the following example, the
method outputs either an
tag for each user “on the fly”: