Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


At a bare minimum, all opening <APPLET> tags must contain the three attributes shown in the preceding examples: CODE, HEIGHT, and WIDTH. These are known as required attributes, as shown in Table 3.1, because you can’t include applets in Web pages without them.

Table 3.1 Required Applet Attributes

Attribute Description

CODE Specifies the name of the applet class file
HEIGHT Specifies the height of the applet in pixels
WIDTH Specifies the width of the applet in pixels

In addition to the three required attributes, you can use various optional attributes to control how an applet appears in your pages. You can include these optional attributes, listed in Table 3.2, anywhere within the opening tag. I recommend, however, that you specify optional attributes after the three required ones to increase the readability of your HTML source code (the exception to this rule is CODEBASE, which should come before the CODE attribute if you use it).

It’s time to show you a complete example for using the <APPLET> tag. The following HTML code shows you what an <APPLET> tag looks like for the MyApplet program:

<APPLET CODE="MyApplet" HEIGHT=100 WIDTH=200 ALT="This is a cool applet"
HSPACE=10 VSPACE=25>
Table 3.2 Optional Applet Parameters

Attribute Description

ALIGN Specifies where your applet is placed on the page in respect to the text around it; it can be one of the following nine alignments: left, right, top, text top, middle, absmiddle, baseline, bottom, and absbottom. The most common alignments (left, top, and right) are shown in Figure 3.1.
ALT Specifies alternate text to be displayed by Java-savvy browsers that are incapable of executing the applet for whatever reason. Note that this text is seen only by Java-savvy browsers because it falls within the opening <APPLET> tag, which all non-Java browsers skip over. If you want to communicate with non-Java browsers, do so using alternate HTML. ALT helps not only those who are running browsers incapable of displaying Java, but also those who have turned the capability off because of limited horsepower. If you present a convincing argument, those users might be persuaded to turn the feature back on “just this once” to see your applet run.
CODEBASE Specifies the base URL for your applet. The applet itself must be located relative to this URL. If CODEBASE isn’t specified, the applet is expected to reside in the same directory as the Web page itself. We’ll talk more about CODEBASE later.
HSPACE Specifies the horizontal space surrounding your applets.
NAME Specifies the symbolic name of your applet, allowing other applets embedded in the same page to locate your applet by name. This attribute is used only when applets on a page communicate with one another, something most applets don’t do.
VSPACE Specifies the vertical space surrounding your applet.


Figure 3.1  The most common alignments: left, top, and right.


Tip:  After you begin adding optional attributes to the mix, the opening tag can become quite difficult to read. To further increase the readability of your HTML source code, I recommend placing any optional attributes on their own line:
<APPLET CODE="MyApplet" HEIGHT=100 WIDTH=200
    ALT="This is a cool applet"
    HSPACE=10
    VSPACE=25>

The browser doesn’t care how the tag appears, as long as it begins with < and ends with >. As a result, you can format your opening tag in any way you want.

CODEBASE

Under normal circumstances, the browser expects to find the applet file inside the same directory as the Web page itself. In this case, you must ensure that the applet file and Web page in which it is embedded share the same directory.

However, keeping the applet and the Web page in the same directory isn’t always possible. What if the applet you want to embed in your page resides halfway around the world? In this case, it’s physically impossible for your Web page and the applet to reside in the same directory. Figure 3.2 shows a picture of a hypothetical server that has three HTML pages. Each of these HTML pages uses two of the three applets that are stored in the Html/Applets directory.


Figure 3.2  Three HTML pages, each of which uses two of the three applets located in the Html/Applets directory.

And what if you want a bunch of different pages on your Web site to use the same applet? What a profound waste of time and Web server space it would be to have to upload a copy of the applet into every directory containing a Web page that used it. A better idea would be to have the applet reside in a central location on your server, where all pages can get to it. But how?

Fortunately, the optional CODEBASE tag does just that. It allows you to specify a URL that points to the directory containing your applet. When a Java-savvy browser encounters the CODEBASE attribute, it automatically knows to look for the applet in whatever directory that attribute points to. The URL you supply for CODEBASE can point to a directory on your server, or one on any other server on the Web:

<APPLET CODEBASE="http://www.infinitevision.net/applets/"
    CODE="MyApplet" HEIGHT=100 WIDTH=200>

In this example, browsers won’t look for the MyApplet applet inside the same directory as the Web page containing this <APPLET> tag. Instead, browsers expect the applet to be located on the Infinite Vision Technologies server, inside the applets directory.

The CODEBASE tag is particularly helpful when a number of pages on your site use the same applet. Rather than having multiple copies of the same applet scattered all around your server, you can place a single copy of the applet in one directory and specify the appropriate CODEBASE attribute in all pages. Not only does this take the headache out of creating these pages, because you no longer have to upload a copy of the applet for every page that uses it, but it also makes upgrading the applet a cinch—simply upgrade the single applet and you’re done.

The URL you supply for CODEBASE can be either relative or absolute.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.