There's More than One Way to Activate
an Applet
NOTE:
This is a new version of an earlier article titled "Introducing
Java Plug-in," which now resides in The Swing Connection
Archive. The main difference
between the two articles is that the sample program supplied with
this article uses Swing's current set of package names, while
the applet supplied with the old article used an older set of
package names that were used prior to the introduction of Swing
1.1 Beta 3 and JDK 1.2 RC1.
By Mark Andrews
This article introduces the power of JavaTM
Plug-in -- a powerful software technology that simplifies the distribution
and use of Swing applets.
Java Plug-in -- a powerful downloadable
software package that's available free from Sun -- provides a simple,
foolproof way to run applets containing Swing applets on either
of the leading-brand browsers.
This article explains how Java
Plug-in works and uses a working sample applet to show you how you
can create and distribute your own applets using Java Plug-in. If
you can't wait until you get to the end of this article to see how
the sample applet works, you can get a preview right now by clicking
the Java Plug-in button.
NOTE: This article was written for people
who want to know some of the basic details about how Java Plug-in
works. If you don't care much about the technology but just want
to start distributing applets using Java Plug-in, there's an easier
way to do it. To learn about that method, see the article in this
section titled "Using
the Java Plug-in Converter."
IMPORTANT:
The sample applet used in this article was written and compiled
using Swing 1.1.1 beta 1. You can run it by using Swing 1.1 beta
3 or later, or by downloading and using JDK 1.2 RC1 or later. But
you can't use it with older versions of Swing or the JDK because
because they used a set of Swing package names that are now outdated.
If you'd like to examine an earlier version of the applet, which
will run under older versions of Swing and the JDK, see the article
titled "Introducing
Java Plug-in" that now resides in The Swing Connection
Archive.
How Java Plug-in Works
If you open a Web page that contains a Java Plug-in-powered
applet, and you have Java Plug-in installed on your computer system,
you donŽt have to do anything special to activate the applet:
It simply opens and runs automatically.
You donŽt even have to worry about how your systemŽs
CLASSPATH
variable is set, because Java Plug-in works independently of such
things as environment variables.
If you open a Web page that contains a Java Plug-in-powered
applet, and you don't have Java Plug-in installed, a dialog
box opens automatically, and you get an invitation to download
a special Java Plug-in engine from Sun. (Of course you must be connected
to the Internet for this invitation to appear.)
If you agree to have Java Plug-in installed on your
system, an installer application runs automatically, and from then
on, youŽre a Java Plug-in user; until you change computers, youŽll
never have to install Java Plug-in again.
To create applets that make use of Java Plug-in, and
to place them on Web pages that you publish, you have to know something
about how Java Plug-in works, and what happens when a user opens
a Web page that contains an applet powered by Java Plug-in.
This article provides all the information youŽll need
about how Java Plug-in works. It describes a couple of different
methods for distributing and activated applets powered by Java Plug-in,
and it concludes with a hands-on demonstration of Swing applet powered
with Java Plug-in.
This section also contains two related articles:
Java Plug-in
IsnŽt the Only Answer
Java
Plug-in, as powerful as it is, may not meet the needs of every
applet that you want to distribute. For one thing, Java Plug-in
is designed to be downloaded from a network, but some computers
don't have network connections. And you might find it difficult
to adapt Java Plug-in to certain combinations of browsers and
operating systems that are sometimes found in multiuser corporate
computer environments.
In situations such as these, several alternatives
to using Java Plug-in are available. To learn how to implement
some of them, read the article in this section titled "Make
Your Browser Swing!".
Using Java Plug-in
There's
nothing special about the way a Java Plug-in applet is written:
Any applet can be accessed, loaded, and executed using Java Plug-in.
All you need to run an applet using Java Plug-in is some special
HTML code for the Web page on which the applet appears.
Later
in this article, youŽll get a chance to open and execute a simple
animated applet using Java Plug-in. The picture on the right is
a screen shot of the applet that you'll be executing.
To run the applet -- or any other applet powered by Java Plug-in
-- all youŽll have to do is open a Web page that contains
some special HTML code for running Java Plug-in applets.
First, though, weŽll cover some basic information
about how Java Plug-in works, and what developers have to do to
create and distribute applets using Java Plug-in.
About Java Plug-in
The
Java Plug-in software package is a mechanism for creating and distributing
applets written in the JavaTM
programming language. Java Plug-in lets you distribute applets using
Swing in such a way that the applets will run automatically on recent
versions of either of the two leading-brand browsers (Netscape or
Internet Explorer). Java Plug-in technology is particularly useful
for distributing applets written with Swing because not all browsers
have Swing support built in.
Java Plug-in is implemented as a downloadable JRE
(Java Runtime Engine). To use Java Plug-in with Swing, all you have
to do is write an applet that makes use of Swing components, and
then add some special HTML code to the Web page that contains your
applet.
Then, when the user of a Netscape or Internet Explorer
browser opens the page containing your applet, one of two things
can happen, depending on the state of the user's system.
If the reader of your Web page has never run an applet
using Java Plug-in, some prompts pop up, followed by a network download
(if the user OKs one). The Plug-in code needed to run your applet
is then downloaded, and your applet executes automatically.
From that point on, the user's system will automatically
run any applet that is powered with Java Plug-in -- without presenting
any more dialogs or performing any more downloads.
Once the Java Plug-in JRE is installed, a Java Plug-in
applet runs just like any other applet. Internally, applets powered
by Java Plug-in use the runtime that's downloaded with Java Plug-in
instead of using the default JavaTM
virtual machine that's built into the user's Netscape or Internet
Explorer browser.
If you'd like more details about Java Plug-in, you
can find a wealth of general and technical information at java.sun.com
and on the Java
Plug-in home page.
Java Plug-in
and Browser Versions
It
doesn't matter what your system's CLASSPATH
setting is when you use Java Plug-in, because Java Plug-in pays
no attention to CLASSPATH
environment variables. Also, it doesn't make much difference what
version you have of the Netscape or Internet Explorer browser
when you use Java Plug-in because Java Plug-in always uses the
version of JDK that the Plug-in is pointing to. Nonetheless, it's
always best, of course, to use the latest versions of the browsers
for other tasks related to the Java programming language that
the browser may need to perform.
For more information about versions of Netscape
browsers and how they work with Swing, see the section headed
"Versions of Netscape browsers" in the article in this section
titled "Make
Your Browser Swing!".
A Brief History
of Java Plug-in
Sun
announced the premiere of Java Plug-in on April 30. The product
is available now as a free download from the Java
Plug-in Web page.
Java Plug-in contains the Swing component set as part
of its core. Consequently, when you download Java Plug-in, you also
get Swing. From then on, you can use Swing in any applets that you
distribute using Java Plug-in.
Future releases of the Java Plug-in software will
also bundle other Java platform products, including Java Development
Kit 1.2 and the ultra-fast Java HotSpotTM virtual machine.
Apple and Linux, too
Apple Computer has announced plans to port
Java Plug-in to Netscape Navigator on the Macintosh, delivering
full Java programming language compatibility to enterprises running
Navigator on Macintosh desktops.
Also, independent Linux developers have created a
runtime Java Plug-in for Linux. It can be downloaded at no cost
from www.blackdown.org/java-linux.html.
Creating a Java
Plug-in applet
On
an HTML page, the only thing that distinguishes a Java Plug-in applet
from any other applet is a difference in tagging. When you place
a Java Plug-in applet on a Web page, you don't use the familiar
<APPLET> and </APPLET> tags. Instead, you must use special
kinds of tags and a special set of syntax rules.
If you arenŽt interested in
writing your own Java Plug-in HTML code, but would rather let the
JavaTM HTML Converter tool to
generate it for you, you neednŽt bother to read the rest of this
article. Instead, feel free to jump to the article titled "Using
The Java Plug-in Converter," which
shows how you can use the Converter tool to generate your Java Plug-in
HTML code automatically.
If you do want to learn more
about how Java Plug-in works, and would like to learn how to write
Java Plug-in HTML code yourself, read on.
Using
Java Plug-in with IE Browsers
Before
we begin our discussion of how to write Java Plug-in HTML code,
thereŽs one important fact you should be aware of. Because the two
leading brands of browsers implement plug-ins in different ways,
you have to more than just substitute a different tag for the <APPLET>
and </APPLET> tags when you want to implement a Java Plug-in
applet. Instead, there are two different conventions for implementing
Java Plug-in applets on HTML pages, and you must learn both of them
if you want to build Java Plug-in power into the applets you create
and distribute.
First, let's assume that you
want to implement a Java Plug-in applet in a Web page that will
be run only on Internet Explorer browsers -- never on Netscape
Navigator browsers. That is an unlikely scenario, of course -- it's
far more likely that you'll want your applets to run on both kinds
of browsers.
But let's take one step at a
time, and assume for now that we're interested only in the Internet
Explorer browser. Later in this article, we'll expand our horizons
to include both the IE and the Netscape Navigator (or Communicator)
browsers.
The
<OBJECT> and </OBJECT> Tags
To embed a Java Plug-in
applet in a Web page that is destined to be displayed using an Internet
Explorer browser, you must delimit the code that implements your
applet with the tags <OBJECT> and </OBJECT>, instead
of with the more familiar tags <APPLET> and </APPLET>.
Example
1
shows how the <OBJECT> and </OBJECT> tags might be used
to execute a Java Plug-in applet from a page displayed on an Internet
Explorer browser.
Example
1
Using the <OBJECT> and </OBJECT> tags
<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
width="120" height="160" align="middle"
codebase="http://java.sun.com/products/plugin/1.2/
jinstall-12-win32.cab#Version=1,2,0,0">
<PARAM NAME="code" VALUE="PlugInApplet.class">
<PARAM NAME="codebase" VALUE="../src/">
</OBJECT>
Syntax notes
As Example 1 illustrates,
the syntax used by the <OBJECT> tag is a little different
from the syntax that the <APPLET> tag requires. One important
difference is that the <OBJECT> tag expects you to use the
construct
PARAM NAME="xxx"
VALUE="xxx"
to
pass parameters to your applet. In Example
1, the parameters passed to the PlugInApp program are:
<PARAM NAME="code" VALUE="PlugInApplet.class">
<PARAM NAME="codebase" VALUE="../src/">
Using the <OBJECT> tag
You may also notice
that in Example 1,
the <OBJECT> tag itself contains a series of interesting elements:
- A classid, which always
assigned the value clsid:8AD9C840-044E-11D1-B3E9-00805F499D93.
This insanely long number is a unique value that is always the
same because it identifies what follows as a Java Plug-in. Because
it never changes, you can copy it right out of this page and
paste it into your own applets.
- A set of attributes identifying such
things as the Java Plug-in applet's height, width, and alignment
-- for example,
width="120"
height="160" align="left"
- A codebase parameter that specifies the URL from which
the Java Plug-in JRE will be downloaded if it does not already
exist in the user's system. Don't be confused; this codebase
parameter is completely different from the codebase parameter
that was passed to PlugInApp using the PARAM NAME tag back in
Example 1.
Because of a confusing coincidence, these two items just happen
to have the same name. The codebase parameter that's
used inside the <OBJECT> tag -- like the classid
parameter described in the first item in this list -- never
changes. So you can copy it, too, and paste it into your own
Java Plug-in applets.
- A version number.
That's all we'll have to say about the <OBJECT>
tag right now. If you'd like more details, you can find them at
http://java.sun.com/products/plugin/1.1/docs/tags.html
Using Java Plug-in
with Netscape Browsers
Now
that you know how to incorporate a Java Plug-in applet into an Internet
Explorer browser, let's take a brief look at a similar procedure
for implementing a Java Plug-in applet from a Netscape Navigator
browser.
When we've finished doing that, we'll combine both
techniques and see what it takes to create and distribute a Java
Plug-in applet that can be executed from either kind of browser.
The <EMBED> and </EMBED>
tags
If you want to equip an HTML page with a Java
Plug-in applet that be executed from a Netscape Navigator browser,
you must use a completely different pair of tags: <EMBED>
and </EMBED>. Example 2 is a code
fragment that shows how you can use the <EMBED> tag to implement
a Java Plug-in applet from a page displayed by a Netscape Navigator
browser.
Example
2
Using the <EMBED> and
</EMBED> tags
<EMBED
type="application/x-java-applet;version=1.2"
width="120" height="160" align="middle"
code="PlugInApplet.class"
codebase="../src/"
pluginspage="http://java.sun.com/products/plugin/1.2/
plugin-install.html">
<NOEMBED>
No JDK 1.2 support found!
</NOEMBED>
</EMBED>
If
you understood the code presented in
Example 1,
you should have no trouble understanding code shown in Example
2. But there are a couple of new points worth
mentioning:
- Along with the <EMBED> and </EMBED>
tags, Netscape recognizes a corresponding pair of <NOEMBED>
and </NOEMBED> tags. They provide content for browsers
that don't recognize the <EMBED> tag, as well as error
messages that display if an <EMBED> tag fails to work.
- A pluginspace item. This item
works just like the codebase item following the word
OBJECT that was shown back in Example
2. It specifies the URL from which
the Java Plug-in JRE will be downloaded if it does not already
exist in the user's system.
Running
Java Plug-in Applets
So
far, we have seen how to implement a Java Plug-in applet on an Internet
Explorer browser, and how to embed the same applet in a page running
on a Netscape Navigator browser. Now, as promised, we'll show how
you can create and distribute Java Plug-in applets that will run
on either kind of browser.
To
learn how to pull this off, there are two facts you must remember:
- Netscape Navigator ignores the <OBJECT>
and </OBJECT> tags, along with everything that appears
between them.
- Internet Explorer ignores the <COMMENT>
and </COMMENT> tags, along with everything that appears
between them.
You'll learn why these two facts
are significant -- and why they are important to Java Plug-in programmers
-- under the subheadings that follow.
Navigator Ignores
the <OBJECT> Tag
As mentioned earlier,
when you want to implement a Java Plug-in applet in HTML code that
is destined to be run on an Internet Explorer browser, you substitute
the <OBJECT> and </OBJECT> tags for the familiar <APPLET>
and </APPLET> tags.
That brings up an important
question: What happens when you implement a Java Plug-in applet
on an HTML page using the <OBJECT> tag and then someone decides
to run the page on a Netscape Navigator browser?
The answer is -- nothing.
Why not? Because, as mentioned
previously, Netscape Navigator ignores
the <OBJECT> tag and everything inside it. Consequently, whenever
you want to implement a Java plug-in applet from an HTML page, you
can safely do so by using the <OBJECT> and </OBJECT>
tags. When you do that, your applet's implementation code will execute
when your HTML page is displayed on an Internet Explorer browser,
but will be ignored (safely) when your HTML page is loaded into
a Netscape Navigator browser.
So now you know how
to display a Java Plug-in applet on IE. All that's left to be concerned
with now is how to get the same thing to happen when your HTML page
is displayed on Netscape Navigator (or Communicator) browser.
IE Doesn't Ignore the <EMBED> Tag
As mentioned earlier, when you want to implement
a Java Plug-in applet from a Netscape Navigator browser, the tags
you must use to delimit your applet are <EMBED> and </EMBED>.
You may be able to guess the
next question that arises: What happens when you implement a Java
Plug-in applet on an HTML page using the <EMBED> tag and then
someone decides to run the page on an Internet Explorer browser?
Don't try to answer this one
too quickly, because the answer is -- the results are not defined.
Why not? Because Internet Explorer
doesn't treat Navigator's <EMBED> tag nearly as nicely as
Navigator treats IE's <OBJECT> tag. You can't just place a
Java Plug-in applet between a pair of <EMBED> and </EMBED>
tags and expect IE to do the right thing. IE doesn't recognize the
<EMBED> and </EMBED> tags, and that's why the results
of using them are not predictable.
Fortunately, though, there is
a large loophole that you can drive your Java Plug-in applet through
to remedy this situation. That loophole is called the <COMMENT>
tag.
You can use the <COMMENT>
and </COMMENT> tags with Internet Explorer because IE ignores
the <COMMENT> tag, the </COMMENT> tag, and everything
that appears between them. So if you use the <COMMENT> to
comment out everything everything that's intended for Netscape eyes
only, you can safely use the <EMBED> and </EMBED> tags
to implement Java Plug-in applets that will be processed by Netscape
Navigator (or Communicator) and will be ignored by IE browsers.
Example
3 shows
how to implement a Java plug-in applet that will run correctly on
both kinds of browsers because of the way it uses the <OBJECT>,
<EMBED>, and <COMMENT> tags.
Example
3
Combining the <OBJECT>, <EMBED>, and <COMMENT>
tags
<OBJECT
classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
width="120" height="160" align="middle"
codebase="http://java.sun.com/products/plugin/1.2/
jinstall-12-win32.cab
#Version=1,1,2,0">
<PARAM NAME="code" VALUE="PlugInApplet.class">
<PARAM NAME="codebase" VALUE="../src/">
<COMMENT>
<EMBED type="application/x-java-applet;version=1.2"
width="120" height="160" align="middle"
code="PlugInApplet.class"
codebase="../src/"
pluginspage="http://java.sun.com/products/plugin/
1.2/plugin-install.html">
<NOEMBED>
</COMMENT>
No support for
JDK 1.2 applets found!
</NOEMBED>
</EMBED>
</OBJECT>
In
this example, Internet Explorer recognizes everything that appears
between the <OBJECT> and </OBJECT> tabs, except for
the material delimited by the <COMMENT> and </COMMENT>
tags. Netscape Navigator ignores the <OBJECT> tag and all
the <PARAM NAME> tags that follow it, but processes everything
that appears between the <EMBED> and </EMBED> tags.
Conclusion
So
there you have it! An applet that works under both popular brands
of browsers: Netscape Navigator and Internet Explorer.
Because the purpose of this article was to teach you
some of the basic theory behind Java Plug-in, it showed you some
techniques that are a little complicated. To learn about an easier
way to use Java Plug-in, see the article in this section titled
"Using the Java Plug-in
Converter."
To get a first-hand look at
how the applet you have been examining works, click the
Java Plug-in button and and experiment all you like. When
the page containing the applet opens, you can download the Java
Plug-in JRE (if you haven't already) by following the instructions
inside the dialogs that appear on your screen. Then you can run
the applet. You can also examine or download all the HTML and Java
code that makes it work, by
just clicking the appropriate buttons.
|