In this example you will learn how to use J-Integra® to access a sample Windows COM+ component from a Java Servlet on your local machine using only Java classes to access the deployed COM+ component.
Accessing COM+ components from a Java Servlet can be extremely difficult for Java programmers. J-Integra® makes this easy by providing Java proxies that enable you to access your COM+ components with no knowledge of COM or DCOM required.
Java Servlets running inside any Web Server that supports a standard JVM are able to use J-Integra® to make requests to your Business Applications COM components. The Web server can itself of course be running on any platform and your COM+ component can be running on any Windows machine that you can access over the network.
You should be familiar with Java Servlets and be able to install and configure your web server for access to the sample Java Servlet.
Download and install J-Integra®.
Make sure the jintegra.jar is included in your CLASSPATH environment variable.
Download and install JDK 1.2.x or higher.
This example assumes you are using Windows 2000.
Copy the Java Servlet code, compile and copy the class file to the webserver
Using Microsoft Management Console create your COM+ component using the ActiveX dll
Note: If you do not have access to Microsoft VB 6.0 IDE or greater, you can use the provided SimpleActiveX.dll in jintegra\examples\servlet-com\ServletToComPlus directory.
Open Visual Basic.
Select ActiveX dll and click Open
Copy the following code into the text box of the new class file and set the class name to Pythagoras. This file is called "pythagoras.cls" and is included in your jintegra\examples\servlet-com\ServletToComPlus directory when you download J-Integra®.
Rem Example of an ActiveX component that can be Rem embedded in Com+ and referenced by JIntegra Rem Simple Example only proving concept. Rem Calculate hypotenuse by reference Function HypotenuseByRef(A As Single, B As Single) As String HypotenuseByRef = Sqr(A ^ 2 + B ^ 2) End Function Rem Calculate hypotenuse by value Function HypotenuseByVal(ByVal A As Single, ByVal B As Single) As String HypotenuseByVal = Sqr(A ^ 2 + B ^ 2) End Function |
From the File menu, select Save Pythagoras.cls
Select a target directory and save the class file.
From the File menu select Save Project
Select the target directory and name the project SimpleActivex.vbp
From the File menu select Make SimpleActivex.dll.
Click OK
Copy the following code in a file and call it ComPlusExample.java. This file is included in your jintegra\examples\servlet-com\ServletToComPlus directory when you download J-Integra®.
import java.io.*; import java.text.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import simpleactivex.*; public class ComPlusExample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { PrintWriter out = response.getWriter(); try { com.linar.jintegra.Cleaner.trackObjectsInCurrentThread(); response.setContentType("text/html"); out.println("<html>"); out.println("<head>"); out.println("<title>"); out.println("Demonstration of Simple Java to Com+"); out.println("</title>"); // p1 and p2 are the string values for the traingle vertices String p1 = request.getParameter("v1"); String p2 = request.getParameter("v2"); // v1 and v2 are the floating point values for the triangle vertices Float v1 = null; Float v2 = null; boolean badValues = false; // try and format the user input try { v1 = p1 == null ? null : new Float(p1); v2 = p2 == null ? null : new Float(p2); } catch (NumberFormatException nfe) { badValues = true; } // See if we need to ask for data input if (p1 == null || p2 == null || badValues) { // Build a form asking for two values to use in the calculation out.println("<body>Uses a Com+ (VB) instance to calculate the length of the" + " hypotenuse for a Right angled triangle using Pythagoras theorem." + "<br>Calls are made by reference, value and using the method name, to show" + "<br>different ways of calling exposed methods." + "<br>Your input values should be between " + Float.toString(Float.MIN_VALUE) + " and " + Float.toString(Float.MAX_VALUE)); out.println("<form>Enter your two values and then click submit"); out.println("<INPUT NAME=v1><INPUT NAME=v2><INPUT TYPE=SUBMIT VALUE=Submit></form></body></html>"); return; } String responseName = "_FAILED"; // Change this line for your appropriate Domain, UserName and Password // Note that you can also allow JIntegra to authenticate the current user // (windows only) by ensuring that your Jintegra/bin directory is on your // path. // com.linar.jintegra.AuthInfo.setDefault("Domain", "username", "password"); // Create the instance. You would normally store this in the Context // rather than recreate each time. final Pythagoras pythagoras = new Pythagoras(); // This is an example of a direct method call exposed by JIntegra // Call the methods directly by reference and value String byVal = pythagoras.hypotenuseByVal(v1.floatValue(), v2.floatValue()); String byRef = pythagoras.hypotenuseByRef( new float[]{v1.floatValue()}, new float[]{v2.floatValue()}); // This is an example of calling a method using its name // Call the methods using the method names. Again refeence and value. String byValName = (String) pythagoras.invokeMethodByName( "hypotenusebyVal", new Object[]{ v1,v2}); String byRefName = (String) pythagoras.invokeMethodByName( "hypotenusebyRef", new Object[]{ v1,v2}); // Print out the results out.println("<br>The Com+ Application calculated the hypotenuse length" + " for a right angled triangle with<br>"); out.println("side 1 = " + v1 + ", side 2 = " + v2 + " is "); out.println("<br>vtable call (by Value) = " + byVal); out.println("<br>vtable call (by Ref) = " + byRef); out.println("<br>IDispatch call (by Value) =" + byValName); out.println("<br>IDispatch call (by Ref) = " + byRefName); out.println("</head>"); out.println("<body bgcolor=\"white\">"); out.println("</body>"); } catch (Exception e) { PrintWriter pw = new PrintWriter(out); e.printStackTrace(pw); pw.flush(); } finally { com.linar.jintegra.Cleaner.releaseAllInCurrentThread(); } } } |
Compile "ComPlusExample.java" along with the J-Integra®-generated proxies using your Java development tool or using the JDK javac tool from the command line. (Remember that you will need to have servlet.jar and jintegra.jar to compile the sample Java Servlet and J-Integra® proxy source files).
In order to have access to a DCOM object, you need to be authenticated. Please see Security/Authentication for information on authentication and security.
Copy the Java ComPlusExample.class file and all the J-Integra® Java proxy classes in the simpleactivex directory to your chosen Servlet installation directory for your installed web server.
Click the Start button, then click Settings, and then click Control Panel. In the Control Panel, double-click the Administrative Tools icon and then double click the Component Services icon to open Component Services.
Add a new COM+ Application by right-clicking on COM+ Applications, and selecting New>Application. Select Install New Application, and use the name JintegraApplication.
Use the defaults for all remaining options and the JIntegraApplication application will then have two subfolders named Components and Roles.
Add the ActiveX component by right clicking on Components, and select New>Component. Select Install New Component, then click Browse. Find the SimpleActiveX dll and double-click it. Use the default options for the following selections and the Components folder will then show some sub-components.
Change the security by right-clicking JIntegraApplication, and then clicking Properties. Click the Security tab. Under Authentication level for calls, select Connect. (Note: J-Integra® only supports "Connect" authentication mode.) Click OK.
Start the JIntegraApplication by right-clicking on JIntegraApplication and selecting Start.
For information on deploying your servlet or JSP to your application server, see the following procedures.
Deploying Servlets |
Deploying JSPs |
Depending upon your web server configuration, you may need to change the path to the ComPlusExample. You should see a page that asks you to enter two values with which to calculate the length of the hypotenuse of a right-angle triangle.
Enter two values and click Submit Query, and you should see a response.
When running under Windows, J-Integra® can use native code to pick up your current login identity. If you would rather that J-Integra® did not do this, or if you are not logged in to an NT domain (for example if your Java code is on a UNIX box), then you must configure DCOM access to the COM+ component, and you must specify the NT domain, user and password to be used by the J-Integra® runtime.
Proceed to Configuring DCOM for Remote Access
You have installed a VB ActiveX COM+ Application and by using J-Integra® you have enabled a Java Servlet to create an instance of the COM+ application and used this instance to perform some simple calculations that can be displayed by your Servlet.
Although this is a very trivial example, it demonstrates the power of J-Integra®. Remember that your COM+ Application can be any suitable COM component and can be running on a local or remote Windows computer or access a remote COM+ Application from a non-windows computer.