This article may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. To maintain the flow of the article, we've left these URLs in the text, but disabled the links.
|
java911@microsoft.com Download the code (10KB) |
CGI Joe to the Rescue!
The JavaASPTest.asp HTML file defines a form with a text input field called InputValue: |
<html>
<body>
<h1>Java ASP Test</h1>
<form method="post" action="Handler.asp">
Enter a value to pass to Java:<p>
<input name="InputValue" value="2" type="text" size="37">
<input type="submit" value="submit">
<form>
<hr>
</body>
</html>
The initial value of the InputValue field is 2. The HTML form's method is set to post, and the action that is executed when the user hits Return (or pushes the form's submit button) is to invoke the Handler.asp page.
<html>
<body>
<h1>Handler!</h1>
<%
'Create a Handler object
Set obj = Server.CreateObject("Java911.JavaASPHandler")
obj.handleRequest Request, Response
%>
<hr>
</body>
</html>
This invokes the Java Virtual Machine (VM), which loads the JavaASPHandler class and returns a new instance of it as a COM object. The reference returned by Server.CreateObject is then saved in the VBScript variable named obj. This object reference is then used to call obj's handleRequest method, passing it the Request and Response objects for the current scripting context. These Request and Response objects areyou guessed it!COM objects. These particular COM objects can be conveniently accessed through a set of Java interfaces defined in com.ms.asp. The interfaces in this package enable Java code to read information from an HTML form (in this case, the form that was defined in JavaASPTest.asp) and to compose a response.
Java Tip of the Month For easy access to the Windows® registry, go with the Microsoft® SDK for Java 2.0. Just import com.ms.lang.* and check out the sources in your %windir%\java\classes\com\ms\lang directory |
Now that I've got the form data, I can compose an HTML response. I do this by first converting the Variant information held in v to a String (by calling v.toString) to produce the original value. I then convert the same Variant to an integer (by calling v.toInt), multiply the result by two, and concatenate the doubled value onto the end of the response string. Finally, I convert the whole response string to a Variant and pass that value to IResponse.Write, which appends the value to the page's HTML response buffer. When the flow of control returns to the browser, the Java-composed HTML response created by handleRequest will appear as part of the Handler.asp page.
After building the JavaASPHandler class with jvc or javac, it needs to be registered as a COM object and copied into %windir%\java\trustlib so that the VM can find it. You can do this from the command line like this:
|
A batch file called reg.bat supplied with the online source code performs these two commands. Once you have registered the JavaASPHandler class, you must copy JavaASPTest.asp and Handler.asp into an appropriate directory so that IIS can publish the files. In the default installation of IIS, a good place would be C:\Inetpub\wwwroot. You can then invoke the example page by starting up Microsoft Internet Explorer and surfing to http://localhost/JavaASPTest.asp. Figures 3 and 4 show the pages you should see. |
Figure 3: JavaASPTest.asp | Figure 4: Handler.asp |
Java Resource of the Month Want to test drive that new Java book before you buy it? Developer.com has a very cool online reference library that includes the full text of many popular Java books http://www.developer.com/reference/r_java.html |
The other way for two applets on the same page to communicate with one another is to look each other up through the AppletContext class, shown in Figure 7. Applet 3, shown in Figure 8, first calls the getAppletContext method to obtain an AppletContext interface. By calling the getApplet method of the AppletContext interface, Applet 3 can look up the Applet4 object (shown in Figure 9) by name. The name for an applet can be specified in the HTML <applet> tag, as shown in Figure 10. Once Applet 3 has a reference to Applet 4, it can cast that object to the SetText interface (which Applet 4 implements) and call Applet 4's setText method. Unfortunately, communication between two applets that are not on the same page in the same browser is not quite as easy. The best and most general approach would be to have the applets communicate through the server using socket connections.
|
|