All Categories :
ActiveX
Chapter 15
Using Java Applets with VBScript
CONTENTS
Everything consists of objects. This statement is true in the
physical world where things are put together by combining other
things. A bicycle consists of handlebars, two spoked wheels, a
frame, a chain, a seat, pedals, safety reflectors, and brakes.
Oops-there also should be an old baseball card tucked into the
spokes.
Everything in the computer world also consists of objects, according
to one way of thinking. Object-oriented programming is
a term you might have encountered before. Also known as OOP, it
is a way to organize computer software as a collection of objects.
For instance, a word processing program could include a dictionary
object that is used by a spell-checking object to correct errors
in a document object.
A World Wide Web page can also be considered a collection of objects.
Each resource on the page-images, video, and programs such as
ActiveX controls and Java programs-is an object. One of these
resource objects that can add a great deal of functionality to
a Web page is a Java applet.
Applets are Java programs designed to run on the World Wide Web.
Like ActiveX and VBScript, Java is an attempt to provide safe
executable content on the Internet. Of the thousands of applets
created to run on the Web, many are publicly available at sites
such as Gamelan (http://www.gamelan.com) and Java Boutique
(http://www.j-g.com/java/).
You can place Java applets on a Web page with the use of an HTML
tag called <APPLET>, as you will discover in this
chapter. Another way that will soon become available to offer
Java programs is the <OBJECT> tag.
The <OBJECT> HTML tag places an object on a Web
page. The object can be any media that someone would want to put
on a page, whether it's an image, movie, program, or other offering.
<OBJECT> was proposed by the World Wide Web Consortium,
the group that establishes many standards for the Web, as a way
to replace a bunch of different things-the <IMG>
tag, the <APPLET> tag used by Java programs, the
DYNSRC attribute used by Microsoft for audio and video,
and other proprietary extensions to HTML. <OBJECT>
was also proposed to handle future forms of Web media not yet
devised. If a startup company in Rancho Cucamonga concocts a way
to offer scratch-and-sniff on the Web, the <OBJECT>
tag is designed to be broad enough to handle it.
Attributes are used with the <OBJECT> tag to specify
the following information:
- The object's name
- The type of object
- The location where it can be found (its URL)
- Layout information such as height, width, spacing, border
width, and alignment
- An ID code to verify its identity
If the object has parameters to establish configurable values,
you can set them with the <PARAM> tag. This tag
has two attributes: NAME and VALUE. The NAME
attribute sets the name of the parameter, and VALUE gives
that parameter a value. Using the <PARAM> tag is
comparable to using the form in the ActiveX Control Pad to set
up a property in an ActiveX control.
If you used the ActiveX Control Pad to add an ActiveX object to
a Web page, an <OBJECT> tag was added automatically
to the page. The following is an example of an ActiveX control's
<OBJECT> tag:
<OBJECT ID="CommandButton1" WIDTH=96 HEIGHT=32
CLASSID="CLSID:D7053240-CE69-11CD-A777-00DD01143C57">
<PARAM NAME="Caption" VALUE="Click Me">
<PARAM NAME="Size" VALUE="2540;846">
<PARAM NAME="FontCharSet" VALUE="0">
<PARAM NAME="FontPitchAndFamily" VALUE="2">
<PARAM NAME="ParagraphAlign" VALUE="3">
<PARAM NAME="FontWeight" VALUE="0">
</OBJECT>
This <OBJECT> tag creates an ActiveX command button
control with the label Click Me. You can place Java applets
on a Web page with HTML code similar to this. Although the <OBJECT>
tag is not yet supported in Microsoft Explorer for Java applets,
the <APPLET> tag is very similar to <OBJECT>.
Although you can put ActiveX controls on a Web page automatically
by using the ActiveX Control Pad, you must place Java applets
by hand by typing the HTML code into a Web page using a text editor.
There's no facility within the ActiveX Control Pad-or any other
authoring tool catering to Internet Explorer 3.0's new features-that
enables automated placement of Java applets using the new <OBJECT>
tag.
Don't complain. When your great-grandchildren are using their
cerebral brainboards to telepathically design their home pages,
you'll have something to brag about. To offer active Web content
back in the 20th century, you had to tap out ASCII characters
on a keyboard-40 miles in the snow, uphill both ways, in a patchwork
coat made from discarded soup labels.
In the next section, you'll learn how to use the <APPLET>
tag to add an object-a Java applet-to a Web page. If you're an
experienced Java programmer, this first task will be review material
for you.
Java is a programming language developed by Sun Microsystems that
was first released to the public in late 1994. It has several
selling points that have made it a popular choice for software
development in the past few years, especially for software that
has Internet-oriented capabilities.
The language is object-oriented, a programming strategy that was
developed to make it easier to reuse elements of a program in
other programs. It also was devised to make software more reliable
and easier to debug.
Another aspect of Java's design is that it enables programs to
be platform independent. Like a World Wide Web page, the ideal
circumstance for a Java program is that it be usable on any computer
platform without a single modification.
The biggest attention-getter for Java is that you can run it on
the World Wide Web. Java programs called applets, which
are typically smaller than other types of programs, are offered
on Web pages just like text, images, ActiveX controls, and other
things.
A Java applet is created with any text editor and compiled into
a pre-executable form called bytecode. You cannot run a
bytecode program in the manner of other exe-cutable programs (such
as .EXE and .COM files, two extensions that are familiar to DOS
users). You must load the bytecode into a Java interpreter that
runs it.
The reason for this two-step process is the platform-independence
advantage mentioned previously. The compiled Java bytecode is
not machine-specific. It needs to remain non-specific so that
it can run without change on all platforms. Each Java-capable
platform-such as Windows 95, Apple Macintosh, and SPARC Solaris
systems at present-has its own Java interpreter that can run bytecode
programs.
Java bytecode files have the .CLASS extension. A Java
applet consists of a main class file and any other class files
that are not part of the standard Java class library. To put a
Java applet on a Web page, you put the applet's class files on
the Web with .HTML files.
The original way to offer an applet on a Web page was to use the
<APPLET> HTML tag. When this tag is encountered
by a Java-capable Web browser such as Netscape Navigator 2.02
for Windows 95 or Internet Explorer 3.0, the browser loads the
applet's class files and executes the program as part of the page.
If you managed to use the World Wide Web without seeing a Java
applet at this point, you've accomplished something, because the
Web today has hundreds of Java applets. A recent check of the
AltaVista search engine found more than 4,200 pages that include
a byte or two of Java.
Sun Microsystems makes Java programming tools freely available
on the World Wide Web. It offers the Java Developer's Kit (JDK)-a
set of command-line tools to compile, debug, and run Java programs-on
the Web at the following URL:
http://java.sun.com/java.sun.com/products/JDK/index.html
If you do not already have the JDK, you don't need to download
and install it right now. It is more important to focus on how
to incorporate existing Java applets into Web pages. The class
file of a sample applet, Mercutio.class, is located on
this book's CD-ROM in the \SOURCE\CHAP15 subdirectory.
You can use Mercutio.class for this task.
In case you want to develop the Java applet or see what a simple
applet looks like, the steps are covered here.
Using any text editor, enter the text of Listing 15.1. The blank
lines and indentation used in this listing are not important;
the Java compiler ignores them. However, you should include them
anyway because they make the program much easier for all non-computers
to understand.
When you're done, save the file as Mercutio.java. If
you're using a word processor that saves in a special format,
such as Microsoft Word, make sure to save it as a plain ASCII
or text file. The line numbers in the listings are included for
reference in this chapter; they are not part of the Java source
code and should not be typed in.
Listing 15.1. The complete listing of the Java source code
file Mercutio.java.
1: import java.awt.*;
2:
3: public class Mercutio extends java.applet.Applet {
4: Button b = new Button("Speak!");
5: TextField t = new TextField(45);
6: int phrase = 0;
7:
8: public void init() {
9: String bt = getParameter("ButtonTitle");
10: if (bt != null)
11: b.setLabel(bt);
12: add(b);
13: add(t);
14: }
15:
16: public boolean action(Event evt, Object obj) {
17: if (++phrase > 5) phrase = 0;
18: switch (phrase) {
19: case 0:
20: t.setText("If love be rough with you, be rough with
love.");
21: break;
22: case 1:
23: t.setText("Dost thou make us minstrels?");
24: break;
25: case 2:
26: t.setText("Speak but one rhyme, and I am satisfied.");
27: break;
28: case 3:
29: t.setText("Any man that can write may answer a letter.");
30: break;
31: case 4:
32: t.setText("Go, villain, fetch a surgeon.");
33: break;
34: default:
35: t.setText("They have made worms' meat of me.");
36: }
37: repaint();
38: return false;
39: }
40: }
Before you test the applet you just created, I want to explain
the main parts of the Java program. The applet displays a phrase
spoken by Mercutio, one of Romeo's more quotable friends in Romeo
and Juliet. Two user-interface elements-a Speak! button and
an empty text field-are created in lines 4 and 5. Lines 12 and
13 place these elements on the applet window.
Java programs are organized into sections called methods,
which are analogous to subroutines or functions in other languages.
Lines 16 through 39 contain the action() method, which
executes every time a user clicks the button.
The phrase variable, created and initialized in line
6, keeps track of which Mercutio quote to display. Line 17 increments
the value of phrase by 1, and if it is greater than 5,
phrase is reset to 0.
Lines 18 through 36 use a big switch...case statement
to display the right Mercutio phrase, and line 37 causes the applet
window to be redrawn so that the new quote displays in the text
field.
Now that you know what the Mercutio applet does, it's time to
test it.
If you entered the source code of Mercutio.java, you
need to compile it into a bytecode file. You can do this using
the Java Developer's Kit compiler by entering the following line
at a command prompt:
javac Mercutio.java
Compile the program using the JDK compiler or any other Java development
tool that can create bytecode files. A bytecode file called Mercutio.class
is created.
Those of you who are following along without writing the program
should find Mercutio.class on the book's CD-ROM at \source\chapter15
if you haven't already. Put a copy of this file somewhere on your
system's hard disk.
The Mercutio applet's class file is ready to be placed on a Web
page. To do this, create a simple .HTML file and put
the applet on it with a special HTML tag, <APPLET>.
Using any text editor, enter Listing 15.2 and save the file as
Mercutio.html (or Mercutio.htm if your platform
does not support four-character file extensions such as .HTML).
This HTML file should be placed in the same directory as the file
Mercutio.class.
Listing 15.2. The full HTML source code of Mercutio.html.
1: <html>
2: <body>
3: <applet code="Mercutio.class" height=150 width=400>
4: </applet>
5: </body>
6: </html>
Now that you have a compiled applet and an HTML page to put it
on, you can use any Web-capable browser to try it out. Load the
file Mercutio.html into Internet Explorer 3.0, most versions
of Netscape Navigator 2.0 or higher, or the AppletViewer tool
that comes with the JDK.
Figure 15.1 shows the output of the Mercutio applet using Internet
Explorer 3.0.
Figure 15.1 : The output of the Mercutio applet on a
Web page.
In its simplest form, an APPLET tag contains the following
elements:
- The opening <APPLET> tag and closing </APPLET>
tag.
- The CODE attribute, which identifies the name of
the applet class file to load: Mercutio.class.
- The HEIGHT and WIDTH attributes, which define
the size of the applet.
In the next section, you'll create a more complicated <APPLET>
tag for the Mercutio applet in order to see what else you can
set up.
Return to the directory on your system that contains the files
Mercutio.class and Mercutio.html. From that
same directory, create a subdirectory called java, and
move the file Mercutio.class into the new subdirectory.
If you examine the Web page Mercutio.html at this point,
you get an error message stating that the Mercutio class file
could not be found. The browser (or AppletViewer) expects to find
the applet's class file in the same directory as the .HTML
file that contains the applet.
You need to add a new attribute, CODEBASE, to Mercutio's
<APPLET> tag. CODEBASE identifies the
directory that contains the applet's class file. Load the file
Mercutio.html into a text editor, and change it so that
the <APPLET> tag appears as the following:
<applet code="Mercutio.class" codebase="java" height=150 width=400>
After making this change, you can run the Mercutio applet correctly
because the browser knows to look in the java subdirectory.
The CODEBASE attribute does not have to be a relative
path to a subdirectory. It could also be a URL reference, such
as http:
//www.mcp.com/sams.
Before saving Mercutio.html, add the following lines
under the APPLET line you just modified:
<param name="ButtonTitle" value="Next!">
This applet requires a Java-enabled browser.
When you're done, Mercutio.html should look like the
code in Listing 15.3.
Listing 15.3. The modified HTML source code of Mercutio.html.
1: <html>
2: <body>
3: <applet code="Mercutio.class" codebase="java" height=150 width=400>
4: <param name="ButtonTitle" value="Next!">
5: This applet requires a Java-enabled browser.
6: </applet>
7: </body>
8: </html>
After saving your changes, load the Web page into a browser or
AppletViewer. You see that the button now has the label Next instead
of Speak!. The PARAM tag sends parameters to a Java applet.
The NAME attribute gives the parameter a name, and the
VALUE attribute sets the value for that parameter. You
can use as many PARAM tags as you need; if you can configure
a Java applet with 413 different parameters, you can use 413 different
PARAM tags.
You must place all PARAM tags between the opening <APPLET>
tag and the closing </APPLET> tag.
The parameters are sent to a Java applet, but the applet program
must do something with them, or they are ignored. In the source
code of Mercutio.java, the following are lines 9 through
11:
String bt = getParameter("ButtonTitle");
if (bt != null)
b.setLabel(bt);
These statements retrieve the ButtonTitle parameter,
if one was provided by the Web page, and use it to set the label
of the button. If this parameter is not provided, the variable
bt has the value null, and the default text
is used for the button's label: Speak!
The other change you made to Mercutio.html was adding
line 5, which states that a Java-enabled browser is required.
This text is located between the opening and closing <APPLET>
tags, and it displays only on Web browsers that are not Java capable.
You can replace it with any text or HTML tags that you want to
provide as an alternative. For example, a Java applet that displays
an animated company logo could provide a non-animated logo for
the Java impaired, as in the following:
<applet code="ExplodingTortugaMintsLogo.class" height=200 width=300>
<param name="BackGroundColor" value="#00FF00">
<img src="TortugaMintsLogo.gif" height=200 width=300 alt="Tortuga Mints">
</applet>
The GIF image file TortugaMintsLogo.gif displays only
on browsers that do not handle Java applets.
Microsoft Internet Explorer, and perhaps other browsers by the
time you read this, plan to offer the functionality of the <APPLET>
tag with a new <OBJECT> tag. As you learn about
the use of <OBJECT> in the next section, you will
recognize a lot of familiar elements.
Now that you understand how to add a Java applet to a Web page
with the <APPLET> tag, it's time to see how the
new <OBJECT> tag accomplishes the same thing.
Note |
Microsoft Internet Explorer 3.0 documentation includes discussion of how Java applets are implemented with the <OBJECT> tag, but the use of the <OBJECT> tag is not fully implemented at the time of this writing. The description here is based on Microsoft's announced plans and the documentation from the World Wide Web Consortium about the tag.
|
Even after the <OBJECT> tag is fully implemented
for Java use, the <APPLET> tag will be supported
to maintain compatibility with existing Web pages. You can continue
to use <APPLET>, a tag that is supported by most
current versions of Netscape Navigator and Internet Explorer 3.0.
However, if you're using this book to integrate ActiveX and VBScript
into your Web sites, the <OBJECT> tag should be
part of your strategy as well.
At present, the only browser that supports the <OBJECT>
tag in any form is Internet Explorer 3.0. However, because representatives
from Netscape and Spyglass participated in the efforts to create
this HTML extension, it is expected for their browsers to offer
it at some point.
The following is an example of an <OBJECT> tag
in use to place an ActiveX Forms 2.0 command button:
<OBJECT ID="CommandButton1" WIDTH=96 HEIGHT=32 CLASSID="CLSID:D7053240-CE69-
11CD-A777-00DD01143C57">
<PARAM NAME="Size" VALUE="2540;846">
<PARAM NAME="FontCharSet" VALUE="0">
<PARAM NAME="FontPitchAndFamily" VALUE="2">
<PARAM NAME="ParagraphAlign" VALUE="3">
<PARAM NAME="FontWeight" VALUE="0">
</OBJECT>
This HTML code is created automatically by using the Insert ActiveX
Control command in the ActiveX Control Pad. Some aspects of this
code should be familiar to you. The WIDTH and HEIGHT
attributes set the size of the control, and the <PARAM>
tag is used in the same way it was used for Java applets in the
last section. The NAME attribute gives the parameter
a name, such as Size, and the VALUE attribute
gives the named parameter a value, such as 2540;846.
The ID attribute of the <OBJECT> tag gives
the object a name. The name is needed so that VBScript or other
scripting languages can communicate with the object, as you'll
see later in this chapter.
The last new aspect of the <OBJECT> tag from the
preceding example is the CLASSID attribute. The first
part, preceding the colon, indicates what the object is. In this
case, CLSID represents an ActiveX control. The part after
the colon is used to identify the object.
Based on the World Wide Web Consortium's proposal for the <OBJECT>
tag, the following is an example of how you could declare a Java
applet:
<object
id="jvaMercutio"
codebase="java\"
codetype="application/java-vm"
classid="java:Mercutio"
width=400
height=150>
<param name="ButtonTitle" value="Next!">
</object>
Because the support for <OBJECT> remains incomplete
in Internet Explorer and unsupported in any form elsewhere, you
need to use the <APPLET> tag for now to add Java
programs to a Web page.
In almost all cases, a Java applet is an island unto itself. It
runs in the space allotted to it by a Web browser, and it cannot
communicate with other programs or the Web page that called it.
The other elements of the Web page also cannot communicate with
the applet after it is loaded.
VBScript provides a way to make a Java applet a more communicative
part of a Web page. VBScript programs have access to any public
methods or public variables inside the Java program and can use
them to change the performance of the applet.
One example of how you can use this is to change a Java applet's
parameters as the applet is running. You use the <PARAM>
tag with applets to establish settings that affect how a Java
applet operates, but you use this tag only to establish an initial
setting. After the applet begins running, the <PARAM>
tag's work is done.
Take a look at the following HTML code to load an applet:
<applet code="DancingHeadline.class" width=400 height=125>
<param name="Headline" value="Disco lives forever!">
<param name="Blinking" value="Yes">
</applet>
This use of the <APPLET> and <PARAM>
tags loads a Java applet called DancingHeadline.class
and sends it a headline parameter with the value Disco
lives forever!. Another parameter, called blinking,
is set to the value Yes.
The blinking parameter controls whether or not the headline
blinks on and off, a feature in the tradition of the often-loathed
HTML <BLINK> tag. The following is the Java statement
that loads the blinking parameter into its own variable:
String blinkStatus = getParameter("blinking");
If the blinkStatus variable was declared as a public
variable, you can change it using VBScript.
To see how this is done, you will create an HTML page that uses
VBScript to communicate with a Java applet.
The VBScript and Java demonstration you will create is a Java
traffic-light animation that changes in response to the click
of intrinsic HTML <INPUT> buttons.
The compiled class file for this applet, Light.class,
is on this book's CD-ROM in the \SOURCE\CHAP15 subdirectory.
However, Listing 15.4 provides the full source code for Light.java
to show how the applet was designed.
You can use this source code to create and compile the applet,
or if you prefer, you can rely on the copy of Light.class
from the CD-ROM.
Listing 15.4. The full source code of Light.java.
1: import java.awt.*;
2:
3: public class Light extends java.applet.Applet {
4:
5: Image light;
6: boolean red = false;
7: boolean yellow = false;
8: boolean green = false;
9:
10: public void init() {
11: light = getImage(getCodeBase(), "traffic.gif");
12: }
13:
14: public void paint(Graphics g) {
15: g.drawImage(light, 10, 10, this);
16: if (red) {
17: g.setColor(Color.red);
18: g.fillOval(35,13,80,80);
19: }
20: if (yellow) {
21: g.setColor(Color.yellow);
22: g.fillOval(35,110,82,82);
23: }
24: if (green) {
25: g.setColor(Color.green);
26: g.fillOval(33,211,87,87);
27: }
28: }
29:
30: public void turnOn(String color) {
31: red = false;
32: yellow = false;
33: green = false;
34: if (color.equals("red")) red = true;
35: if (color.equals("yellow")) yellow = true;
36: if (color.equals("green")) green = true;
37: repaint();
38: }
39: }
The following things are happening in this applet:
- Lines 5 through 8 declare four variables that are used in
this applet. One variable, Image, stores a graphics file
that is loaded elsewhere. The other three variables-red,
yellow, and green-hold boolean values. They
are equal to true if their specific lights are lit, or
false otherwise. All three begin with a value of false,
so none of the lights are lit.
- Line 11 loads the image file traffic.gif into the
variable light. It is part of the init() method,
which runs automatically when the applet is first loaded.
- Lines 14 through 28 are the paint() method. This
is called any time the applet window needs to be repainted.
- Line 15 displays the image stored in light on the
applet window.
- Lines 16 through 18 check to see whether the red
variable has the boolean value of true. If it does, a
red oval is drawn at the x,y position of (35,13)
with a width of 80 and a height of 80.
- Lines 20 through 22 draw a yellow oval if the yellow
variable equals true.
- Lines 24 through 26 draw a green oval if the green
variable equals true.
- Lines 30 through 37 are the turnOn() method of the
applet, which turns on a light based on the color parameter
sent to the method. If color is equal to red,
the value of the red boolean variable is set to true.
- Line 37 tells the applet that it needs to be repainted, and
the paint() method is called.
To understand how VBScript can interact with Java, you need to
concentrate on the turnOn() method of the Light
applet. You could call this method inside the Java applet with
the following statement:
turnOn("yellow");
This statement results in the yellow boolean variable
being set to true. Nothing is displayed right away because
the paint() method handles all screen updating. However,
when repaint() is called in line 37, the paint()
method displays the traffic light with the yellow light illuminated.
This isn't the only way to change the light on display. The turnOn()
method has a public declaration in front of it, and VBScript
can call it.
Create a new HTML page that will contain the Java applet and some
other elements. Using any text editor, open a file and enter the
text of Listing 15.5. Save the file as traffic.html.
Make sure that the files Light.class and traffic.gif
are in the same directory as traffic.html.
Listing 15.5. The full HTML source code of traffic.html.
1: <html>
2: <head>
3: <title>Traffic Light demo</title>
4: <SCRIPT language = "VBScript">
5: <!--
6: Sub RedButton_OnClick
7: document.jvaLight.turnOn("red")
8: End Sub
9:
10: Sub YellowButton_OnClick
11: document.jvaLight.turnOn("yellow")
12: End Sub
13:
14: Sub GreenButton_OnClick
15: document.jvaLight.turnOn("green")
16: End Sub
17:
18: -->
19: </SCRIPT>
20: </head>
21: <body>
22: <center>
23: <applet code="Light.class" id="jvaLight" height=325 width=148 align="left">
24: </applet>
25: <table cellpadding=30 border=0 align="left">
26: <tr>
27: <td>
28: <input type=button value="Red" name="RedButton">
29: </td>
30: <tr>
31: <td>
32: <input type=button value="Yellow" name="YellowButton">
33: </td>
34: </tr>
35: <tr>
36: <td>
37: <input type=button value="Green" name="GreenButton">
38: </td>
39: </tr>
40: </table>
41: </center>
42: </body>
43: </html>
Most of the syntax of this HTML file should be familiar to you.
The <TABLE> tags arrange the Web page; you do not
need them for any other reason in this example.
Lines 28, 32, and 37 place three intrinsic HTML form buttons on
the page and give them the names RedButton, YellowButton,
and GreenButton.
Lines 23 and 24 place the applet Light.class on the page.
The new attribute to the <APPLET> tag is id="jvaLight".
This attribute gives the Java applet a name that a VBScript command
can use to communicate with the applet.
A short VBScript program in lines 4 through 16 consists of three
OnClick subroutines: RedButton_OnClick, YellowButton_OnClick,
and GreenButton_OnClick. Each of these subroutines calls
the turnOn() method of the Java applet. The following
is an example:
document.jvaLight.turnOn("red")
document.jvaLight identifies the object that VBScript
is communicating with-the Java applet with the ID value
of jvaLight. The last part of the statement, turnOn("red"),
is a call to the turnOn() method inside the applet.
When you load the Web page traffic.html into a browser,
it should resemble the one in Figure 15.2.
Figure 15.2 : The output of the traffic-light demonstration
page.
The traffic-light demonstration shows how VBScript can access
a Java applet's public methods. It is also possible to
set any public variables of an applet. The following
statement shows a VBScript subroutine that sets a Java variable:
Sub SpeedUpButton_OnClick
document.jvaRace.speed = document.jvaRace.speed + 1
End Sub
This example refers to a speed variable in a Java applet
with an ID of jvaRace. speed is increased
by 1 whenever the SpeedUpButton object's OnClick
subroutine is called.
Using public variables from outside a Java applet can
wreak havoc on the program if it has not been designed to handle
the changes. As a part of object-oriented programming, the style
embodied by the Java language, the use of a public variable
means that it should be modifiable at any time by external objects-other
Java programs, VBScript, and the like. If a variable cannot be
changed without causing unexpected side effects or errors, it
should not be declared public.
By working through the parts of this chapter, you have seen Java
in action on a Web page, and you've seen how VBScript can be used
to make it more responsive to other parts of a page.
You have also learned the present and future HTML tags that will
be used for Java applets in Internet Explorer-APPLET
and OBJECT. Java can be offered in conjunction with ActiveX
and VBScript as a way to offer a more compelling and interactive
experience on a Web site.
Now that you have learned about Java and VBScript, the following
chapters are good places to continue:
- To learn more about using intrinsic HTML controls such as
buttons with VBScript, see Chapter 2 "Using VBScript with
HTML Controls."
- Chapter 6 "Checking Form Data," contains more details
about building OnClick procedures in VBScript.
- For more information about using the ActiveX Control Pad,
see Chapter 13, "Implementing an ActiveX Menu Control."
Q: | The <OBJECT> tag has been described as a proposal. Is there a way to track its progress toward official approval?
|
A: | The World Wide Web Consortium maintains a Web site at http://www.w3.org. The current page for the <OBJECT> proposal, which is titled "Inserting Objects into HTML," is at the following URL:
http://www.w3.org/pub/WWW/TR/WD-object
Note, however, that the leading developers of Web browsers, Netscape and Microsoft, adopt most new HTML tags at their own initiative before the World Wide Web Consortium agrees on a standard.
|
Q: | I noticed that ActiveX objects have a long numeric string in their CLASSID attribute such as CLSID:D7053240-CE69-11CD-A777-00DD01143C57. Java applets do not have this in the CLASSID. Why is the <OBJECT> tag implemented differently between ActiveX and Java in regard to CLASSID?
|
A: | The difference comes about because of the way ActiveX and Java implement security. As methods to execute programs over the Web, both ActiveX and Java must provide a reliable way to prevent malicious or unintentionally damaging code from being run. Otherwise, viruses, Trojan horses, and other nasty things could occur on a user's computer when these Web-based programs run.
Java's security model relies on a secure language that does not allow programmers to write malicious code. Applets are restricted in the kinds of things they can do-no reading or writing files on the user's computer, no network connections to other Internet sites, and so on.
The ActiveX security model, on the other hand, generally relies on establishing a way to authenticate that a program comes from a trusted source. A user must approve an ActiveX control before downloading it, and a security certificate is presented to give users the ability to double-check the identity of the ActiveX program.
The different approach to the CLASSID attribute comes about because ActiveX uses that long alphanumeric string as part of its way to identify a control. Java does not implement that kind of security check, so a CLASSID such as java:com.spiderbyte.Light is all that is needed.
|
Q: | My Java applet uses a second class file as part of its function. How can I access the public variables and methods of this second class file if there's no way to name it with an ID attribute?
|
A: | Only classes that derive from Java.applet.Applet can be called or accessed from VBScript. If you want to call a method in another class, you have to create a method in your applet that calls the other class's method.
To reach the public variables in the other class, you have to create a method in the applet class. Think of the applet's class file as the gateway between VBScript and the other class files that compose your Java program.
|