Java Servlet to Com+ Example

Summary

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.

Introduction

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.

Prerequisites

  1. You should be familiar with Java Servlets and be able to install and configure your web server for access to the sample Java Servlet.

  2. Download and install J-Integra®.

  3. Make sure the jintegra.jar is included in your CLASSPATH environment variable.

  4. Ensure that you have installed the J-Integra® license.
  5. Download and install JDK 1.2.x or higher.

  6. Read the Java™ Servlet to COM JSP/Servlet to Excel Example, since it introduces some basic concepts that are assumed here.

This example assumes you are using Windows 2000.

The Steps Involved

  1. Copy the VB sample code and create ActiveX dll

  2. Generate the Java proxies using J-Integra®

  3. Copy the Java Servlet code, compile and copy the class file to the webserver

  4. Using Microsoft Management Console create your COM+ component using the ActiveX dll

  5. Deploy the example
  6. Run the example

  7. Remote Access

Create the VB ActiveX Component

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.

  1. Open Visual Basic.

  2. Select ActiveX dll and click Open

  3. 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®.

  4. 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
    		
  5. From the File menu, select Save Pythagoras.cls

  6. Select a target directory and save the class file.

  7. From the File menu select Save Project

  8. Select the target directory and name the project SimpleActivex.vbp

  9. From the File menu select Make SimpleActivex.dll.

  10. Click OK

Generate Java proxies for the ActiveX Library

  1. Create a sub-directory in the target directory in which you have put your SimpleActivex.dll and call this sub-directory simpleactivex
    Note:
    the name is case sensitive
  2. Start the J-Integra® com2java tool to generate the proxies for SimpleActiveX.dll.
  3. Enter c:\jintegra\examples\ServletToComPlus into the Output Dir field
  4. Enter simpleactivex as the Java package.
    Note:
    the name is case sensitive
  5. Click the Generate Proxies button
  6. Click OK and com2java generates the Java proxy classes.

Compile the Sample Servlet and J-Integra® Proxies

  1. 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®.

  2. 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(); 
        }
      } 
    }

  3. 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).

  4. In order to have access to a DCOM object, you need to be authenticated. Please see Security/Authentication for information on authentication and security.

  5. 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.

Creating the COM+ Component

  1. 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.

  2. Add a new COM+ Application by right-clicking on COM+ Applications, and selecting New>Application. Select Install New Application, and use the name JintegraApplication.

  3. Use the defaults for all remaining options and the JIntegraApplication application will then have two subfolders named Components and Roles.

  4. 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.

  5. 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.

  6. Start the JIntegraApplication by right-clicking on JIntegraApplication and selecting Start.

Deploying the Example

For information on deploying your servlet or JSP to your application server, see the following procedures.

Deploying Servlets

Deploying Servlets to J2EE

Deploying Servlets to JRun

Deploying Servlets to TomCat

Deploying Servlets to WebSphere

Deploying JSPs

Deploying JSPs to J2EE

Deploying JSPs to JRun

Deploying JSPs to TomCat

Deploying JSPs to WebSphere

Run the example

  1. 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.

  2. Enter two values and click Submit Query, and you should see a response.

Remote Access

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

Conclusion

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.