[Enterprise Extensions only]

Examples: Accessing Enterprise JavaBeans without a J2EE client container

The following code extracts can be used to access Enterprise JavaBeans through the ActiveX to EJB bridge without 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.

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 an InitialContext using the specified provider URL and context factory
  3. Accesses an Enterprise JavaBean home of the HelloEJB sample provided with WebSphere Application Server
  4. 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.

Note: Because the code extract does not use a J2EE client container, it sends the JNDI name to XJBGetEJBHome() instead of using a java:comp resource name. The code also needs the WebSphereSamples.HelloEJB.HelloHome class stubs in its classpath (which is not illustrated here). The stubs can be extracted from the Enterprise JavaBean JAR file. For information about how to change the classpath, see the XJBInit Visual Basic helper function.
' Some Globals:
Dim oXJB 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
	' and uses values from our environment that was initialized by launchClientXJB.bat
	' or setupCmdLineXJB.bat 
	If oXJB Is Nothing Then
        Set oXJB = XJBInit(Environ("WAS_HOME"), Environ("JAVA_HOME"), Environ("NAMING_FACTORY"))
    End If

    ' Create the initial context
    ' - This creates a J2EE initial context.
    ' - We pass a Hashtable via the use of a JMethodArgs object to NewInstance
	    Dim oArgs as Object
    Set oArgs = oXJB.GetArgsContainer
    
    ' Build our properties using the normal naming string constants.  Here they are
    ' static Fields of the javax.naming.Context object
    Dim Context_PROVIDER_URL As String
    Dim Context_INITIAL_CONTEXT_FACTORY As String
    Context_PROVIDER_URL = oXJB.FindClass("javax.naming.Context").PROVIDER_URL
    Context_INITIAL_CONTEXT_FACTORY = oXJB.FindClass("javax.naming.Context").INITIAL_CONTEXT_FACTORY
    
    ' We connect to MyServer.com and pull the Initial Context Factory out
    ' of an environment variable which was set in launchClientXJB.bat.
    Dim oHT As Object
    Set oHT = oXJB.NewInstance(oXJB.FindClass("java.util.Hashtable"))
    XJBPutProperty oXJB, oHT, Context_PROVIDER_URL, "iiop://MyServer.com"
    XJBPutProperty oXJB, oHT, Context_INITIAL_CONTEXT_FACTORY, Environ("NAMING_FACTORY")
    
    Dim initialContext As Object
    Set initialContext = oXJB.NewInstance(oXJB.FindClass("javax.naming.InitialContext"), oHT)

    ' Get EJB Home
    ' We specify the following:
    ' - InitialContext: Our J2EE InitialContext
    ' - JNDIName:       The JNDI Name of our EJB using
    ' - 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, "WSsamples/HelloEJBHome", _
                                  "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 an InitialContext using the specified provider URL and context factory
  3. Accesses an Enterprise JavaBean home of the HelloEJB sample provided with WebSphere Application Server
  4. Calls a method on> the Enterprise JavaBean

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

Note: Because the code extract does not use a J2EE client container, it sends the JNDI name to XJBGetEJBHome() instead of using a java:comp resource name. The code also needs the WebSphereSamples.HelloEJB.HelloHome class stubs in its classpath (which is not illustrated here). The stubs can be extracted from the Enterprise JavaBean JAR file. For information about how to change the classpath, see the XJBInit ASP helper function.
<-- #include virtual ="/WSASPIncludes/setupASPXJB.inc" -->

<%
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
	' and uses values from our environment that was initialized in our
	' setupASPXJB.inc include file
	    Application.lock
    If IsEmpty(Application("oXJB")) Then
        Set Application("oXJB") = XJBInit(com_ibm_websphere_washome, com_ibm_websphere_javahome,com_ibm_websphere_namingfactory)
    End If
    Application.unlock

    ' Create the initial context
    ' - This creates a J2EE initial context.
    ' - We pass a Hashtable via the use of a JMethodArgs object to NewInstance
	    Dim oArgs
    Set oArgs = Application("oXJB").GetArgsContainer
    
    ' Build our properties using the normal naming string constants.  Here they are
    ' static Fields of the javax.naming.Context object
    Dim Context_PROVIDER_URL
    Dim Context_INITIAL_CONTEXT_FACTORY
    Context_PROVIDER_URL = Application("oXJB").FindClass("javax.naming.Context").PROVIDER_URL
    Context_INITIAL_CONTEXT_FACTORY = Application("oXJB").FindClass("javax.naming.Context").INITIAL_CONTEXT_FACTORY
    
    ' We connect to MyServer.com and pull the Initial Context Factory out
    ' of an environment variable which was set in launchClientXJB.bat.
    Dim oHT
    Set oHT = Application("oXJB").NewInstance(Application("oXJB").FindClass("java.util.Hashtable"))
    XJBPutProperty Application("oXJB"), oHT, Context_PROVIDER_URL, "iiop://MyServer.com"
    XJBPutProperty Application("oXJB"), oHT, Context_INITIAL_CONTEXT_FACTORY, com_ibm_websphere_namingfactory
   
    Dim initialContext
    Set initialContext = Application("oXJB").NewInstance(Application("oXJB").FindClass("javax.naming.InitialContext"), oHT)

    ' Get EJB Home
    ' We specify the following:
    ' - InitialContext: Our J2EE InitialContext
    ' - JNDIName:       The JNDI Name of our EJB using
    ' - 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, "WSsamples/HelloEJBHome", _
                                  "WebSphereSamples.HelloEJB.HelloHome")
    
    ' Create the EJB Home
	    set hello = helloHome.create
   
    ' Execute a method on our EJB.
    Response.Write(hello.getMessage())
End Sub
%>