Using applet, object and embed Tags


 

This chapter includes the following topics:

Introduction

This chapter explains to HTML authors how and when to use the applet, object, and embed tags to add Java applets to Web pages. In addition, this chapter provides guidelines for deploying applets on the Internet and Intranets, and for use with different browsers.

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, read the W3 HTML specification.

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. Sun therefore recommends that you continue to use the applet tag as a consistent way to deploy Java applets across browsers on all platforms.

Following is an example of the applet tag:

    <applet code=Applet1.class width="200" height="200">
    Your browser does not support the <code>applet</code> tag.
    </applet> 

For both Internet Explorer and the Mozilla family of browsers, if Java Plug-in is installed (version 1.3.1_01a or later) then the latest installed version of Java Plug-in is invoked to run the applet.

Note: You cannot use the applet tag to automatically download a JRE if one is not installed locally.

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 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 latest 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 specific version of Java Plug-in to be used.

For example, to use Java Plug-in version 1.5.0, you specify:

classid="clsid:CAFEEFAC-0015-0000-0000-ABCDEFFEDCBA"

The codebase Attribute

You use the optional codebase attribute to specify if and how to download the JRE.

The codebase attribute has two forms:

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.

Sun has packaged each version of the JRE installer in Microsoft cabinet (.cab) file format. You can view 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.5.0"
  pluginspage="http://java.sun.com/j2se/1.5.0/download.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:

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:CAFEEFAC-0015-0000-0000-ABCDEFFEDCBA"
  <param name="code" value="Applet1.class">
    <comment>
      <embed code="Applet1.class"
        type="application/x-java-applet;jpi-version=1.5.0">
        <noembed>
          No Java Support.
        </noembed>
      </embed>
    </comment>
  </object>

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 "Netscape", 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 == 'Netscape') {
    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>

You can use the HTML Converter tool to help with generating object and embed tags for mixed environment examples.