The <APPLET> Tag

Applets are embedded in HTML documents with the <APPLET> tag. The <APPLET> tag resembles the HTML <IMG> image tag. It contains attributes that identify the applet to be displayed and, optionally, give the web browser hints about how it should be displayed.[50] The standard image tag sizing and alignment attributes, such as height and width, can be used inside the applet tag. However, unlike images, applets have both an opening <APPLET> and a closing </APPLET> tag. Sandwiched between these can be any number of <PARAM> tags that contain data to be passed to the applet:

<APPLET attribute 
            attribute ... >
    <PARAM parameter >
    <PARAM parameter >
    ...
</APPLET>

Attributes

Attributes are name/value pairs that are interpreted by a web browser or appletviewer. (Many HTML tags besides <APPLET> have attributes.) Attributes of the <APPLET> tag specify general features that apply to all applets, such as size and alignment. The definition of the <APPLET> tag lists a fixed set of recognized attributes; specifying an incorrect or nonexistent attribute should be considered an HTML error.

Three attributes are required in the <APPLET> tag. Two of these attributes, width and height, specify the space the applet occupies on the screen. The third required attribute may be either code or object; you must supply one of these attributes, and you can’t specify both. The code attribute specifies the class file from which the applet is loaded; the object attribute specifies a serialized representation of an applet. Most often, you’ll use the code attribute; the tools for creating serialized applets aren’t quite there yet.

The following is an HTML fragment for a hypothetical simple clock applet that takes no parameters and requires no special HTML layout:

<APPLET code=AnalogClock width=100 height=100></APPLET>

The HTML file that contains this <APPLET> tag must be stored in the same directory as the AnalogClock.class class file. The applet tag is not sensitive to spacing, so the previous code is therefore equivalent to:

<APPLET  
    code=AnalogClock  
    width=100  
    height=100>  
</APPLET>

You can use whatever form seems appropriate.

Parameters

Parameters are analogous to command-line arguments; they provide a way to pass information to an applet. Each <PARAM> tag contains a name and a value that are passed as strings to the applet:

<PARAM name = parameter_name value = parameter_value>

Parameters provide a means of embedding application-specific data and configuration information within an HTML document.[51] Our AnalogClock applet, for example, might accept a parameter that selects between local and universal time:

<APPLET code=AnalogClock width=100 height=100>  
    <PARAM name=zone value=GMT>  
</APPLET>

Presumably, this AnalogClock applet is designed to look for a parameter named zone with a possible value of GMT.

Parameter names and values can be quoted to contain spaces and other special characters. We could therefore be more verbose and use a parameter value like the following:

<PARAM name=zone value="Greenwich Mean Time">

The parameters a given applet expects are determined by the developer of that applet. There is no fixed set of parameter names or values; it’s up to the applet to interpret the parameter name/value pairs that are passed to it. Any number of parameters can be specified, and the applet may choose to use or ignore them as it sees fit. The applet might also consider parameters to be either optional or required, and act accordingly.

¿Habla Applet?

Web browsers ignore tags they don’t understand; if the web browser doesn’t interpret the <APPLET> or <PARAM> tags, they should disappear and any HTML between the <APPLET> and </APPLET> tags should appear normally.

By convention, Java-enabled web browsers do the opposite and ignore any extra HTML between the <APPLET> and </APPLET> tags. This means we can place some alternative HTML inside the <APPLET> tag, which is displayed only by web browsers that can’t run the applet.

For our AnalogClock example, we could display a small text explanation and an image of the clock applet as a teaser:

<APPLET code=AnalogClock width=100 height=100>  
    <PARAM name=zone value=GMT>  
    <strong>If you see this you don't have a Java-enabled Web  
    browser. Here's a picture of what you are missing.</strong>  
    <img src="clockface.gif">  
</APPLET>

The Complete <APPLET> Tag

We can now spell out the syntax for the full-blown <APPLET> tag:

<APPLET  
    code = class_name

or:

object = serialized_applet_name  

    width = pixels_high
    height = pixels_wide

    [ codebase = location_URL ]  
    [ archive = comma_separated_list_of_archive_files ]  
    [ name = applet_instance_name ]  
    [ alt = alternate_text ]  
    [ align = style ]  
    [ vspace = vertical pad pixels ]  
    [ hspace = horizontal pad pixels ]  
>
    [ <PARAM name = parameter_name value = parameter_value> ] ...

... [ HTML code for non-Java-aware browsers ]  
</APPLET>

Either the code attribute or the object attribute must be present to specify the applet to run. The code attribute specifies the applet’s class file; you’ll see this most frequently. The object attribute specifies a serialized (pickled) representation of an applet. When you use the object attribute to load an applet, the applet’s init( ) method is not called. However, the serialized applet’s start( ) method is called.

The width, height, align, vspace, and hspace attributes deterine the preferred size, alignment, and padding, respectively. The width and height attributes are required.

The codebase attribute specifies the directory to be searched for the applet’s class file (or archive file). If this attribute isn’t present, the browser looks in the same directory as the HTML file. The archive attribute specifies a list of JAR or ZIP files in which the applet’s class file is located. To put two or more files in the list, separate the filenames by commas; for example, the following attribute tells the browser to search three archives for the applet:

archive=Part1.jar,Part2.jar,Utilities.jar

The archive attribute must be present if you have packaged your applet in an archive. When searching for classes, a browser checks the files listed in the archives attribute before searching any other locations on the server.

The alt attribute specifies alternate text that is displayed by browsers that understand the <APPLET> tag and its attributes, but can’t actually run applets. This attribute can also describe the applet, since in this case any alternate HTML between <APPLET> and </APPLET> is, by convention, ignored by Java-enabled browsers.

The name attribute specifies an instance name for the executing applet. This is a name specified as a unique label for each copy of an applet on a particular HTML page. For example, if we include our clock twice on the same page (using two applet tags), we should give each instance a unique name to differentiate them:

<APPLET code=AnalogClock name="bigClock" width=300 height=300></APPLET>
<APPLET code=AnalogClock name="smallClock" width=50 height=50></APPLET>

Applets use instance names to recognize and communicate with other applets on the same page. We could, for instance, create a “clock setter” applet that knows how to set the time on an AnalogClock applet and pass it the instance name of a particular target clock on this page as a parameter. This might look something like:

<APPLET code=ClockSetter>  
    <PARAM name=clockToSet value="bigClock">  
</APPLET>

Loading Class Files

The code attribute of the <APPLET> tag should specify the name of an applet. This is either a simple class name, or a package path and class name. For now, let’s look at simple class names; we’ll discuss packages in a moment. By default, the Java runtime system looks for the class file in the same location as the HTML document that contains it. This location is known as the base URL for the document.

Consider an HTML document, clock.html, that contains our clock applet example:

<APPLET code=AnalogClock width=100 height=100></APPLET>

Let’s say we retrieve the document at the following URL:

http://www.time.ch/documents/clock.html

Java tries to retrieve the applet class file from the same base location:

http://www.time.ch/documents/AnalogClock.class

The codebase attribute of the <APPLET> tag can be used to specify an alternative base URL for the class file search. Let’s say our HTML document now specifies codebase, as in the following example:

<APPLET  
    codebase=http://www.joes.ch/stuff/  
    code=AnalogClock  
    width=100  
    height=100>  
</APPLET>

Java now looks for the applet class file at:

http://www.joes.ch/stuff/AnalogClock.class

Packages

Packages are groups of Java classes. A package name is a little like a URL, in that they both use a hierarchical, dot-separated naming convention. The full name of a Java class file is formed by prefixing the class name with the package name.

In addition to providing a naming scheme, packages determine the storage locations of class files. Before a class file is retrieved from a server, its package-name component is translated by the client into a relative path name under the base URL of the document. The components of a class package name are turned into the directory components of a pathname, just as with classes on your local system.

Let’s suppose that our AnalogClock has been placed into a package called time.clock (a subordinate package for clock-related classes, within a package for time-related classes). The fully qualified name of our class is time.clock.AnalogClock. Our simple <APPLET> tag would now look like:

<APPLET code=time.clock.AnalogClock width=100 height=100></APPLET>

Let’s say the clock.html document is once again retrieved from:

http://www.time.ch/documents/clock.html

Java now looks for the class file in the following location:

http://www.time.ch/documents/time/clock/AnalogClock.class

The same is true when specifying an alternative codebase:

<APPLET  
    codebase=http://www.joes.ch/stuff/  
    code=time.clock.AnalogClock  
    width=100  
    height=100>  
</APPLET>

Java now tries to find the class in the corresponding path under this base URL:

http://www.joes.ch/stuff/time/clock/AnalogClock.class

One possible package-naming convention proposes that Internet host and domain names be incorporated as part of package names to form a unique identifier for classes produced by a given organization. If a company with the domain name foobar.com produced our AnalogClock class, they might distribute it in a package called com.foobar.time.clock. The fully qualified name of the AnalogClock class would then be com.foobar.time.clock.AnalogClock. This would presumably be a unique name stored on an arbitrary server. A future version of the Java class loader might use this to automatically search for classes on remote hosts. Perhaps soon we’ll run Sun’s latest and greatest web browser directly from its source with:

% java com.sun.java.hotjava.HotJava

Viewing Applets

Sun’s SDK comes with an applet-viewer program, aptly called appletviewer. To use appletviewer, specify the URL of the document on the command line. For example, to view our AnalogClock at the URL shown ealier, use the following command:

% appletviewer http://www.time.ch/documents/clock.html

appletviewer retrieves all applets in the specified document and displays each one in a separate window. appletviewer is not a web browser; it doesn’t attempt to display HTML. It’s primarily a convenience for testing and debugging applets. If the document doesn’t contain <APPLET> tags, appletviewer does nothing.



[50] If you are not familiar with HTML or other markup languages, you may want to refer to HTML: The Definitive Guide, by Chuck Musciano and Bill Kennedy (O’Reilly & Associates), for a complete reference on HTML and structured web documents.

[51] If you are wondering why the applet’s parameters are specified in yet another type of tag, here’s the reason. In the original alpha release of Java, applet parameters were included inside of a single <app> tag along with formatting attributes. However, this format was not SGML-compliant, so the <PARAM> tag was added.

Get Learning Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.