Accessing Java from ASP in DCOM Mode |
In this example you will see how you can easily access your unmodified Java classes running in any JVM from Active Server Pages programs. The example takes the form of a step-by-step tutorial, which essentially anyone can try out. It should take you less than an hour to run through this example.
You will see how you can easily access local and remote Java classes running anywhere that supports a standard JVM, such as Java 2 on UNIX. You can access all public fields and methods (including static ones) in Java objects, without modifying the Java classes:
Add C:\jintegra\bin to the machine's PATH environment variable.
First, create an ASP page with the following contents:
Using VBScript:
<%@ LANGUAGE = VBScript %> <HTML> <HEAD> <TITLE>Accessing Java classes from ASP using any JVM running anywhere</TITLE> <BODY> <% ' Create an instance of the DemoClass Java class, which can be sitting in ' any JVM on any OS. You can use Server.CreateObject() too (see below). Set Demo = GetObject("aspjvm:DemoClass") ' To use Server.CreateObject, use regprogid to register a progid first. ' Set Demo = Server.CreateObject("aspjvm.DemoClass") ' Invoke a method which performs a calculation FVal = Demo.CalcFV(0.1, 12, -10, -19, 1) Response.write("FVal = " & FVal & "<BR>") ' Invoke a method which returns another object. Set aCollection = Demo.getValues() ' aCollection is a Java java.util.Vector. Dim s s = aCollection.Size For i = 1 To s Response.write("Value = " & aCollection.get(i - 1) & "<BR>") Next ' Finally access a public field (which happens to be static) Response.write("Demo.name = " + Demo.name & "<BR>") Demo.name = "Alfred" Response.write("Updated Demo.name = " & Demo.name & "<BR>") %> </BODY> </HTML> |
Or using JScript:
<%@ LANGUAGE = JScript %> <HTML> <HEAD> <TITLE>Accessing Java classes from ASP using any JVM running anywhere</TITLE> <BODY> <% // Create an instance of the DemoClass Java class, which can be sitting in // any JVM on any OS. You can use Server.CreateObject() too (see below). Demo = GetObject("aspjvm:DemoClass") ' To use Server.CreateObject, use regprogid to register a progid first. ' Demo = Server.CreateObject("aspjvm.DemoClass") // Invoke a method which performs a calculation FVal = Demo.CalcFV(0.1, 12, -10, -19, 1) Response.write("FVal = ".concat(FVal).concat("<BR>")) // Invoke a method which returns another object. aCollection = new Enumerator(Demo.getValues()) for (; !aCollection.atEnd(); aCollection.moveNext()) { Response.write("Value = ".concat(aCollection.item()).concat("<BR>")) } // Finally access a public field (which happens to be static) Response.write("Demo.name = ".concat(Demo.name).concat("<BR>")) Demo.name = "Alfred" Response.write("Updated Demo.name = ".concat(Demo.name).concat("<BR>")) %> </BODY> </HTML> |
Open the Internet Services Manager and create a virtual directory to put
the ASP file you just created. Give it Read access and set the Execute Permissions
such that it can run scripts:
If you are running ASP under Windows 2000 and the ASP virtual directory
is set to be Pooled (the default), then open the Component Services management
tool, go to COM+ Applications and change the properties for IIS Out-Of-Process
Pooled Applications: set the Authentication Level to Connect in the Security
pane (the default is Packet, which J-Integra® does not currently support).
If you'd prefer not to change this for all pooled apps, configure the virtual
directory to be Isolated, and then change the specific entry for
that Virtual directory that appears under COM+ Applications).
Displays protocol statistics and current TCP/IP network connections to pick a free port number which is not used by any other applications:
netstat -a
For example, you may find that port 3350 is free.
Open a command prompt and run the J-Integra® 'regjvmcmd' command, which is in the C:\jintegra\bin directory. You will need to know the TCP/IP address or the URL of the machine where the Java object which you will be accessing will be running -- where the JVM is. If you will be running the JVM on the local machine, use 'localhost', otherwise specify the appropriate TCP/IP name.
This is the command:
regjvmcmd aspjvm JvmHostingMachine.IP.Address[3350]
aspjvm is the name you wish to use to refer to the JVM. You can use anything you wish.
JvmHostingMachine.IP.Address is the TCP/IP address or the URL of the machine running the JVM, so use localhost if running the JVM locally. Otherwise, use the appropriate name.
3350 is a TCP/IP port number over which communications will take place. You need to specify a free port from the previous step 5.
Create a DemoClass.java file with the following contents:
public class DemoClass { // Public field public static String name = "hello test"; // Public method. Shows use of standard Java types, // and how an object reference can be returned public java.util.Vector getValues() { System.out.println("getValues called"); java.util.Vector values = new java.util.Vector(); values.addElement("A String"); values.addElement(new Double(9876.5432)); values.addElement(this); // !!! values.addElement(new java.util.Date()); return values; } // Taken from MS example public double CalcFV(double dblRate, double dblNPer, double dblPMT, double dblPv, boolean bType) { double dblRet, dblTemp, dblTemp2, dblTemp3; if (dblRate == 0.0) { dblRet = -dblPv - dblPMT * dblNPer; } else { dblTemp = (bType ? 1.0 + dblRate : 1.0); dblTemp3 = 1.0 + dblRate; dblTemp2 = Math.pow(dblTemp3, dblNPer); dblRet = -dblPv * dblTemp2 - dblPMT * dblTemp * (dblTemp2 - 1.0) / dblRate; } return dblRet; } } |
Java objects run inside a Java Virtual Machine. In order to access them from ASP they have to be run somewhere. You have two options: You can either have them run inside a JVM process that you already run, or you can start a new JVM process which will host them.
If you wish to do the latter, then create a small main program, which registers the JVM, and then just hangs around. J-Integra® background threads handle the DCOM requests from the ASP client. This is a suitable program:
public class DCOMBridgeJvm { public static void main(java.lang.String[] args) throws Exception { System.out.println("DCOMBridgeJvm Started..."); com.linar.jintegra.Jvm.register("aspjvm", 3350); while (true) { // Otherwise JVM will exit. Thread.sleep(100000); } } } |
Note that there are no references to the Java classes being accessed. Any public Java class that has a public default constructor, which is in your CLASSPATH, can be accessed from ASP.
As you will no doubt have noticed, the parameters to Jvm.register() are
the JVM name, and the port number used when registering the JVM. If you
do not wish to hard-code the port number, you can just specify the JVM name,
and when you run the Java application set the JINTEGRA_DCOM_PORT property
instead. (i.e. -DJINTEGRA_DCOM_PORT=3350)
กก
Compile the DemoClass and the DCOMBridgeJvm and run it:
C:\Inetpub\wwwroot\test>set
PATH=C:\jintegra\bin;C:\j2sdk1.4.2_01\bin;%PATH%
C:\Inetpub\wwwroot\test>set CLASSPATH=.;C:\jintegra\lib\jintegra.jar;%CLASSPATH%
C:\Inetpub\wwwroot\test>javac *.java
C:\Inetpub\wwwroot\test>java DCOMBridgeJvm
กก
Reset your IIS:
กก
The DCOMBridgeJvm.java must be running before the ASP page accesses the DemoClass.java.
Finally, access the ASP page from your browser:
http://localhost/ASP2Java/DemoClassTest.asp
From the above you can see that the integration is extremely flexible -- ASP to Java renders the Java object visible to ASP as though it were a COM object (in fact it is a COM object, because the J-Integra® runtime loaded into the JVM, remote-enables any Java object, and talks DCOM directly to ASP).
If you wish to have the Java objects run inside an existing JVM, then you will have to make sure that the J-Integra® runtime (jintegra.jar, from the kit), and the classes you wish to access (in this case DemoClass) are in your CLASSPATH, and then add the Jvm.register(...) line to your Java startup code.