Documentation Contents

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

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:

Deploying Applets for Specific Browsers

When deploying applets:

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_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
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 given codebase.
The archives in 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
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 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
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 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
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, and absbottom.
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 the img tag's vspace and hspace 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:
<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.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.

The codebase 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.

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.8"
  pluginspage="http://www.oracle.com/technetwork/java/javase/downloads/ea-jsp-142245.html"/>

The type attribute can have one of two forms:

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:

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.8">
        <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:

  1. Detect the user's browser through the appName variable.

  2. Use the document.write() method to write a tag based on the value of the appName variable:

  1. If the browser name equals "Mozilla", write the embed tag.

  2. 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>

Oracle and/or its affiliates Copyright © 1993, 2019, Oracle and/or its affiliates. All rights reserved.
Contact Us