by Susie Adams
Applets, like ActiveX controls, often provide the ability to accept information from the document or the user at runtime. Applets provide this ability via an interface that allows the customization of applet behavior as it's browsed in a Web browser. You can accomplish this customization in two ways: first, input parameters and second, public methods and/or instance variables.
In the past, ActiveX developers using Java applets had only input parameters available to them. Unlike ActiveX controls, there was no way to call a method within an applet class with ActiveX script. The introduction of Microsoft's ActiveX Runtime for Java has changed this situation. Thanks to Internet Explorer, the ActiveX Runtime for Java allows access to public Java class methods and variables just as if the applet was an ActiveX control. As a result, your script can manipulate Java applets developed with any tool, not just those developed with Visual J++.
In this article, we'll demonstrate how to create an Active Server page that calls an already existing Microsoft Applet J++ applet. In the first example, we'll call the applet and then set its parameter. In the second example, we'll use ActiveX scripting to call two of its public methods.
To pass the information from an HTML or ASP page to a Java applet you must use the <PARAM> tag. You can place this tag between the HTML <APPLET> and </APPLET> tags. The <PARAM> tag has two attributes, NAME and VALUE, which are used to pass data to a Java applet. Note that Documents can pass multiple parameters.
<APPLET CODE="displaytext.class" WIDTH=100 HEIGHT=100>
<PARAM> NAME="initialvalue" VALUE="Hi Susie">
</APPLET>
The Java applet then retrieves the parameters with the getParameter method:
String x=getParameter("initialvalue");
In the example above, we declare a variable x of type String in the .java file to hold the initialvalue parameter retrieved from the Web document. The parameter name is specified as the argument passed to getParameter in the applet code.
To demonstrate the use of parameters, we'll use a sample applet created by Microsoft called displaytext.class. This applet displays text in a Web page based on either an input parameter passed by the page or by input the user enters at runtime. To begin, let's create a base folder to hold the demo applications we'll build in this article. As you can see in Figure A, we named our folder D:\Cobbsart\JavaApplets\DisplayText.
Figure A
First, create a folder for your project.
To get the .class for this applet, open Visual J++ and select the Info View tab. Next, select the Samples, then Microsoft Samples, and click Scripting: VBScript Driving A Java Applet, as shown in Figure B. Follow the directions to copy the code from the J++ CD to the directory you created on your hard drive.
Figure B
Follow the instructions to load the sample J++ files to your hard drive.
You'll notice a number of files, including two HTML files. Feel free to look at them—but don't cheat. We're going to create the displaytext file as an ASP file later.
Now that we have our sample applet, let's begin by creating a new Web Project named DisplayTextVID. First, select the New option from Visual InterDev's file menu. In the dialog that appears, select the project tabs Web Project option and name the new Web project DisplayTextVID. Store the project in a subdirectory named DisplayTextVID under the demo directory you created earlier. In the dialogs that appear, select your web server and create the new web.
Next, create a new folder in the root of the Web named DisplayText and copy the displaytext.class file into it by right-clicking on the new folder and selecting the Add Files menu option. In the resulting dialog, navigate to the DisplayText directory and select the displaytext.class file. This class doesn't require the support of any additional files.
To call the displaytext.class, first create a new ActiveServer page named display.asp by selecting the Visual InterDev file menu's New option. In the dialog that appears select the file tab's Active Server Page menu option. Then add the following code to the ASP page:
<applet
code=displaytext.class
codebase=DisplayText
width=600
height=120 >
<param name=initialValue value="Hello World">
</applet>
Notice the new PARAM tag. The value of this parameter is passed as the default text to display on the page. When you browse the display.asp page, you should see the text "Hello World" in the Web browser, as shown in Figure C. Open the page, change the parameter value, and browse the page again. The new parameter value should be displayed in the browser.
Figure C
On the browser page, you'll see "Hello World," from display.asp.
If you're familiar with scripting, you've probably already embedded an ActiveX control into a Web page and then manipulated it using VBScript. The ActiveX runtime for Java offers the same functionality for Java applets, making Java applets scriptable too. When a Java applet is running in a browser, all the public methods and variables of the applet automatically become available to VBScript or any other language supporting the ActiveX scripting protocol.
For example, consider the displaytext Java applet we used in the last example:
public class displaytext extends Applet
{
// The value of the text to be displayed //by the applet.
String m_phrase;
public void setText(String string)
{
//Set the text to the string.
}
public void resetText()
{
//reset the text to the original //value.
}
}
In our previous example, we included the applet in the ASP page by using the APPLET tag. To refer to the applet from the ActiveX scripting language you must add the ID attribute to the APPLET tag. This allows you to refer to public classes in an applet as you would refer to methods of an ActiveX control.
<applet>
code=displaytext.class
codebase=DisplayText
id=TextDisplay
width=600
height=120 >
<param name=initialValue value="Hello World">
</applet>
Next, let's add a few user interface items. We'll start with an input field to enter the intialvalue text and two buttons—one to set the text to the input value and one to reset the text to the parameter value originally passed by the parm tag:
<input name=Phrase value="Some new text">
<input type=button name=Set value="Set Display Text">
<input type=button name=Reset value="Reset to Initial Values">
In the scripting portion of your ASP page, define OnClick handlers for each button, allowing them to manipulate the applet:
<script language=VBScript>
<!--
' Handle the click event for the Set button
sub Set_OnClick
' Call the applet setText method,
' with the value in the Phrase <input> box
document.TextDisplay.setText Phrase.value
end sub
' Handle the click event for the Reset button
sub Reset_OnClick
' Call the applet resetText method
document.TextDisplay.resetText
end sub
-->
</script>
Whenever a user clicks a button or enters text in the input field, the appropriate VB Script handler is fired, which then allows the handler to manipulate the Java applet. Note: When you reference the applet from VBScript, you must always reference it by document.name. The complete code for display.asp is shown in Listing A.
Listing A:
The Display.ASP file modified to accept user input from an Input Field
<%@ LANGUAGE="VBSCRIPT" %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual InterDev 1.0">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Document Title</TITLE>
</HEAD>
<BODY>
<!-- Insert HTML here -->
<html>
<head>
<title>Display Text</title>
</head>
<script language=VBScript>
<!--
' Handle the click event for the Set button
sub Set_OnClick
' Call the applet setText method,
' with the value in the Phrase <input> box
document.TextDisplay.setText Phrase.value
end sub
' Handle the click event for the Reset button
sub Reset_OnClick
' Call the applet resetText method
document.TextDisplay.resetText
end sub
-->
</script>
<body>
<applet
code=displaytext.class
codebase=DisplayText
id=TextDisplay
width=600
height=120 >
<param name=initialValue value="Hello World">
</applet>
<p>
<input name=Phrase value="Some new text">
<input type=button name=Set value="Set Display Text">
<input type=button name=Reset value="Reset to Initial Values">
<hr>
</body>
</html>
</BODY>
</HTML>
To view the display.asp page, right-click on the page and select the Browse With menu option. You should now see the page, as displayed in Figure D.
Figure D
The finished display.asp file allows the user to update the text of the message.
Note: Only the Applet-derived class is directly accessible from scripting. If your Java applet includes other classes that you want to access, you must first define public methods in your Applet-derived class that call each of those classes.
As we've demonstrated, the addition of the ActiveX Runtime for Java takes a huge step towards making ActiveX and Java applets playground friends in the Web environment. We saw how Java applets could be driven just like ActiveX controls and how ActiveX script could read and write the public variables of the applet, as well as call its public methods.
You can even implement code that facilitates communication between Java applets and ActiveX controls using event handlers to connect them. Remember, we've only touched the surface of dealing with most Web developers' needs. To accommodate some of these needs, more sophisticated ways of allowing Applets and ActiveX to communicate have been developed. However, all of these methods require a working knowledge of how to use a COM object from Java.
Hopefully, we've just experienced the first wave of a flood of Java applet and ActiveX inter-operability tools that will be available in the near future to cover the gap between the two Web worlds. For now, anyway, these new tools are definitely a step in the right direction!
-----------------------------
Susie Adams is a senior consultant with Financial Dynamics, a client/server and Internet solutions consulting firm in McLean, Virginia. She has worked in the application development area for more than 11 years and currently focuses on the design and development of active content Web applications.
-----------------------------
This article is reproduced from the February 1998 issue of Active Server Developer's Journal. Active Server Developer's Journal is an independently produced publication of The Cobb Group. No part of this article may be used or reproduced in any fashion (except in brief quotations used in critical articles and reviews) without prior consent of The Cobb Group. To contact The Cobb Group, please call (800) 223-8720 or (502) 493-3200.
Copyright © 1998 The Cobb Group, a division of Ziff-Davis Inc. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis is prohibited.