HTML Tags for Applets
Home ] Up ] Introduction to Applets ] A Simple Applet ] [ HTML Tags for Applets ] The Applet Life Cycle ] Multimedia and URLs ] Applet Context ] Converting Applications to Applets ]

 

 

To embed an applet within a web page, you must use an appropriate set of HTML tags:

Here's the HTML for a very simple web page with an embedded applet:

<html>
<head>
<title>I'm a Java-enabled Web Page!</title>
</head>
<body>
 Here's the Java applet:
 <applet code="MyApplet.class"
	 width="300"
	 height="200"
 >
 Sorry, you're out of luck!
 Your browser doesn't support Java.
 </applet>
</body>
</html>

As you can see, there's a single tag, <applet> ... </applet> which has a number of attributes that can be specified.

Here are the named attributes for the <applet> tag for JDK 1.0.x:

Attribute name Required? Description
CODE="foo.class" Yes Specifies the name of the applet's .class file.
WIDTH=pixels Yes Specifies the width of the applet window
HEIGHT=pixels Yes Specifies the height of the applet window
CODEBASE="path" No Specifies where to find the .class files, relative to the location of the enclosing HTML page.
NAME="applet-name" No Specifies the name of the applet (used to distinguish it from other applets on the same page).
ALIGN=LEFT No Places the applet at the left margin of the page. Text that follows goes in the space to the right of the applet.
ALIGN=RIGHT No Places the applet at the right margin of the page. Text that follows goes in the space to the left of the applet
ALIGN=BOTTOM No Places the bottom of the applet at the bottom of the text in the current line.
ALIGN=TOP No Places the top of the applet with the top of the current line.
ALIGN=TEXTTOP No Places the top of the applet with the top of the text in the current line.
ALIGN=MIDDLE No Places the middle of the applet with the baseline of the current line.
ALIGN=ABSMIDDLE No Places the middle of the applet with the middle of the current line.
ALIGN=BASELINE No Places the bottom of the applet with the baseline of the current line.
ALIGN=ABSBOTTOM No Places the bottom of the applet with the bottom of the current line.
VSPACE=pixels No Creates a vertical margin around the applet .
HSPACE=pixels No Creates a horizontal margin around the applet.

Here are the <applet> tag attributes added for JDK 1.1.x:

Attribute name Required? Description
ARCHIVE="jar-file" No Specifies the name of a Java archive (JAR) file that contain classes and other resources for the applet.
OBJECT="obj-file" No Specifies the name of a file that contains the serialized applet object.

Adding Package and Directory Specifications

The above HTML example is very basic:

  • It specifies an applet whose .class file is in the same directory as its enclosing HTML page file.
  • The class whose .class file is being used is in the default package.

Think of the CODEBASE attribute as specifying a kind of CLASSPATH for the applet. The same kind of rules apply to applets as apply to applications, when you start dealing with classes within packages.

To specify an applet whose .class file is in subdirectory classes, and whose class is in the default package:

<html>
<head>
<title>I'm a Java-enabled Web Page!</title>
</head>
<body>
 Here's the Java applet:
 <applet code="MyApplet.class"
	 codebase="classes"
	 width="300"
	 height="200"
 >
 Sorry, you're out of luck!
 Your browser doesn't support Java.
 </applet>
</body>
</html>

To specify an applet whose class is in the package myPackage:

<html>
<head>
<title>I'm a Java-enabled Web Page!</title>
</head>
<body>
 Here's the Java applet:
 <applet code="myPackage.MyApplet.class"
         codebase="classes"
	 width="300"
	 height="200"
 >
 Sorry, you're out of luck!
 Your browser doesn't support Java.
 </applet>
</body>
</html>

and the .class file must be in subdirectory below where the document is:

classes/myPackage

 

This page was last modified on 02 October, 2007