There's More than One Way to Activate
an Applet
This article introduced Java Plug-in when Java Plug-in was first
introduced. The sample applet supplied with the article uses a set
of Swing package names that are now outdated and work only with
older versions of Swing. A new version of the article, "Plugging
into Swing," can be found in the in the current "Swing
and the Web" section in this issue. The sample applet presented
with that article is an up-to-date applet that works under current
versions of Swing.
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.
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 example, 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 multi-user 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 do 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
Java Plug-in --
a new technology that Sun introduced earlier this year -- lets you
distribute applets using Swing in such a way that they will run
automatically on recent versions of either of the two leading-brand
browsers (Netscape or Internet Explorer). (For complete details
on what versions of Netscape and IE are compatible with Java Plug-in,
see the Java
Plug-in home page.)
Java Plug-in is implemented as a downloadable JRE (Java Runtime
Engine). By tapping the power of Java Plug-in, you can create applets
that are always fully compatible with the latest releases of Java
Foundation Classes (a.k.a. Swing), and can then distribute
those applets with the assurance that they will run on any browser.
When you create an applet and place it on a Web page using Java
Plug-in, any user who opens your Web page can choose to run the
applet by simply responding to a prompt. If the user agrees, the
Java Plug-in JRE is then downloaded to the user's computer system.
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 Java virtual machine that's built into the
user's Netscape or Internet Explorer browser.
Java Plug-in and Browser
Versions
When
you use Java Plug-in, it doesn't make much difference what the
version number of your Netscape or Internet Explorer browser is
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 Java-related tasks
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, "Make
Your Browser Swing!". To learn more about versions
of Internet Explorer browsers, visit the IE
Download page on the Microsoft Web site.
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 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.
How it works
The Java Plug-in
software package is a mechanism for creating and distributing applets
written in the JavaTM programming
language. It is particularly useful for distributing applets written
with Swing because not all browsers have Swing support built in.
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.
If the user has obtained a Java Plug-in download on some previous
occasion, the prompts and the download are not repeated; your applet
simply executes immediately, like any other applet written in the
Java programming language.
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.
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 Java Plug-in
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 "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="left"
codebase="http://java.sun.com/products/plugin/1.1/
jinstall-11-win32.cab
#Version=1,1,0,0">
<PARAM
NAME="code" VALUE="PlugInApplet.class">
<PARAM NAME="codebase" VALUE="../assets/applets">
<PARAM NAME="type"
VALUE="application/x-java-applet;version=1.1">
<PARAM NAME="model" VALUE="models/HyaluronicAcid.xyz">
No JDK 1.1 support found!
</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="html/">
<PARAM NAME="type"
VALUE="application/x-java-applet;version=1.1">
<PARAM NAME="model" VALUE="models/HyaluronicAcid.xyz">
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.
Due to 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 I 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.1"
width="120" height="160" align="left"
code="PlugInApplet.class"
codebase="../assets/applets" model="models/Dukie"
pluginspage="http://java.sun.com/products/plugin/1.1/
plugin-install.html">
<NOEMBED>
No JDK 1.1 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, I'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
betweeen 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 I mentioned earlier, when you want 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 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 ignore 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 ignore by IE browsers.
Example
3 shows
how to implement a Java plug-in implementation 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="baseline"
codebase="http://java.sun.com/products/plugin/1.1/
jinstall-11-win32.cab
#Version=1,1,0,0">
<PARAM NAME="code" VALUE="PlugInApplet.class">
<PARAM NAME="codebase" VALUE="../assets/applets/">
<PARAM NAME="type"
VALUE="application/x-java-applet;version=1.1">
<PARAM NAME="model" VALUE="models/HyaluronicAcid.xyz">
<COMMENT>
<EMBED type="application/x-java-applet;version=1.1"
width="120" height="160" left
model="models/HyaluronicAcid.xyz"
code="PlugInApplet.class"
codebase="../assets/applets"
pluginspage="http://java.sun.com/products/plugin/1.1/
plugin-install.html">
<NOEMBED>
</COMMENT>
No support for
JDK 1.1 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.
To see how the applet we 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 JavaTM
code that makes it work, by just clicking the appropriate buttons.
|