[Enterprise Extensions only]

Examples: Accessing Enterprise JavaBeans through a J2EE client container

The following code extracts can be used to access Enterprise JavaBeans through the ActiveX to EJB bridge using a J2EE client container. This code is taken from the samples included with the ActiveX to EJB Client provided with WebSphere Application Server. For information about the samples, see the related samples information.

For equivalent example code to access Enterprise JavaBeans without using a J2EE client container, see Examples: Accessing Enterprise JavaBeans without a J2EE client container.

Visual Basic

In this example, the Visual Basic code extract performs the following actions:

  1. Initializes the ActiveX to EJB bridge and JVM
  2. Creates a J2EE client container
  3. Creates an InitialContext using the namespace generated by the client container
  4. Accesses an Enterprise JavaBean home of the HelloEJB sample provided with WebSphere Application Server
  5. Calls a method on the Enterprise JavaBean

These actions are performed using the example Visual Basic helper functions defined in Examples: Visual Basic helper functions

' Some Globals:
Dim oXJB as Object
Dim oCC as Object

Sub Main()
    ' Initialize the ActiveX to EJB bridge using our Helper function above.
    ' Store it in a global variable.  In this example, it is initialized once only.
    If oXJB Is Nothing Then
        Set oXJB = XJBInit(Environ("WAS_HOME"), Environ("JAVA_HOME"), Environ("NAMING_FACTORY"))
    End If

    ' Initialize the J2EE Client Container using our Helper function above.
    ' Store it in a global variable.  In this example, it is initialized once only
    ' and we use the XMI/XML files from our client Jar stored in Samples.ear.
	' It later connects to a server called MyServer.com and use port 900 (the default).
	' If we had more than one client JAR file in our EAR, then we could specify the name
	' of the Client Jar as the fourth parameter.
	    If oCC Is Nothing Then
        Set oCC = XJBGetJ2EEClientContainer(oXJB, "c:\myearfiles\Samples.ear", "MyServer.com", "900", "")
    End If

    ' Create the initial context
    ' - This creates a J2EE initial context that allows us to use
    '   the java:comp namespace.
    Dim initialContext As Object
    Set initialContext = oXJB.NewInstance(oXJB.FindClass("javax.naming.InitialContext"))

    ' Get EJB Home
    ' We specify the following:
    ' - InitialContext: Our J2EE InitialContext
    ' - JNDIName:       The JNDI Name of our EJB using the java:comp namespace
    ' - HomeClassName:  Name of the home class.  This is pulled out of the
    '                   appropriate EJB jar file within our EAR.
    Dim hello As Object
    Dim helloHome As Object
    Set helloHome = XJBGetEJBHome(oXJB, initialContext, "java:comp/env/ejb/Hello", _
                                  "WebSphereSamples.HelloEJB.HelloHome")
    
    ' Create the EJB Home
    ' Note:  We use CallByName() because Visual Basic likes to 
    'Capitalize the "create" method as Create, which
    ' doesn't really exist
	    set hello = CallByName(helloHome, "create", vbMethod)
   
    ' Execute a method on our EJB.
    MsgBox hello.getMessage()

End Sub

Active Server Pages

In this example, the Active Server Pages code extract performs the following actions:

  1. Initializes the ActiveX to EJB bridge and JVM
  2. Creates a J2EE client container
  3. Creates an InitialContext using the namespace generated by the client container
  4. Accesses an Enterprise JavaBean home of the HelloEJB sample provided with WebSphere Application Server
  5. Calls a method on> the Enterprise JavaBean

These actions are performed using the example ASP helper functions defined in Examples: ASP helper functions

<-- #include virtual ="/WSASPIncludes/setupASPXJB.inc" -->

<%
Sub Main()
	    ' Use an Application lock to ensure that both the ActiveX to EJB bridge and Client Container objects are 
	    ' created in the same thread.
	    Application.Lock

    ' Initialize the ActiveX to EJB bridge using our Helper function above.
    ' Store it in a global variable.  In this example, it is initialized once only.
	' The com_ibm_websphere... variables come from
    ' our setupASPXJB.inc file above.
    If IsEmpty(Application("oXJB")) Then
        Set Application("oXJB") = XJBInit(com_ibm_websphere_washome, com_ibm_websphere_javahome,com_ibm_websphere_namingfactory)
    End If

    ' Initialize the J2EE Client Container using our Helper function above.
    ' Store it in a global variable.  In this example, it is initialized once only
	' and we use the XMI/XML files from our client Jar stored in Samples.ear.
	' It later connects to a server called MyServer.com and uses port 900 (the default).
	' If we had more than one client JAR file in our EAR, then we could specify the name
	' of the Client Jar as the fourth parameter.
	    If IsEmpty(Application("oCC")) Then
        Set Application("oCC")= XJBGetJ2EEClientContainer(Application("oXJB"), "c:\myearfiles\Samples.ear", "MyServer.com", "900", "")
    End If

	    Application.unlock

    ' Create the initial context
    ' - This creates a J2EE initial context that allows us to use the java:comp namespace.
    Dim initialContext
    Set initialContext = Application("oXJB").NewInstance(Application("oXJB").FindClass("javax.naming.InitialContext"))

    ' Get EJB Home
    ' We specify the following:
    ' - InitialContext: Our J2EE InitialContext
    ' - JNDIName:       The JNDI Name of our EJB using the java:comp namespace
    ' - HomeClassName:  Name of the home class.  This is pulled out of the
    '                   appropriate EJB jar file within our EAR.
    Dim hello
    Dim helloHome
    Set helloHome = XJBGetEJBHome(Application("oXJB"), initialContext, "java:comp/env/ejb/Hello", _
                                  "WebSphereSamples.HelloEJB.HelloHome")
    
    ' Create the EJB Home
    set hello = helloHome.create
	     
    ' Execute a method on our EJB.
    Response.Write(hello.getMessage())

End Sub
%>