About EJB client

xiaoxiao2021-03-06  57

Client type

In the WAS5 and J2EE programming environment, there are a variety of EJB clients to access EJB in the system, as follows:

SessionBean

Session beans can be viewed as entity beans client access. Typically, session beans are in the same EJB container in the same EJB container in the same EJB container, and it can be accessed through local interface.

Servlet

Servlet and EJB can interact with remote and local interfaces. A preferred suggestion is to use the servlet to access the session bean, and then interact with the entity bean by session bean. Accessing session beans, servlets can use remote interfaces, or use local interfaces, which is convenient, usually using remote interfaces.

For the front desk user HTML and JSPS calling Servlet, servlet is a thin client tool.

Application Clients

How to access the client

How to type of client access EJB has the following steps:

1, get home interface

2, find and create an EJB instance

3, call EJB instance method

HOME interface

There are two ways to get the HOME interface of EJB.

1. Access to the JNDI name

2. Access to the reference name

These are described in the deployment descriptor;

First, get inittal naming context

The easiest way is to use constructor:

Javax.naming.initialContext IC = New javax.naming.initialContext ();

This method is used to be when EJB is in the same virtual machine or the same machine.

We can create an initian context through two parameters

1, machine name, naming service port (provider_url)

2, InitialContext Factory name (Initial_Context_Factory)

Use java.util.properties to provide parameters (you can also need haShtable)

Java.util.properties Properties = new java.util.properties ();

Properties.Put (javax.naming.context.provider_url, "iiop: ///");

Properties.Put (javax.naming.context.initial_context_factory, "com.ibm.Websphere.naming.wsninitialcontextFactory);

Javax.naming.initialContext IC = New javax.naming.initialcontext (property);

In the local machine, provider_url us use "IIOP: ///"

The actual format is as follows: "IIOP: // Hostname: Port"

The second parameter is different depending on each application server. In WAS5, use com.ibm.websphere.naming.wsninitialcontextFactory, depending on the server.

Second, find the EJB interface from Initial Context

Using Initial Naming Context We can find EJB HOME you want to access.

For remote interfaces, we can use the global JNDI name or EJB reference name, and for local interfaces, we must use a local reference to access.

Use global JNDI name

The code using the global JNDI name to access the remote interface is as follows:

Object objhome = Ic.lookup ("EJB / ITSOBANK / CUSTOMER");

Customerhome Customerhome = (customerHome) javax.rmi.portableremoteObject.Narrow (Objhome, customerHome.Class); EJB / ITSOBANK / Customer is the JNDI name we use in the deployment descriptor when you create EJB.

Use EJB applications

Use the EJB reference to access the remote interface code as follows:

Object objhome = Ic.lookup ("Java: Comp / ENV / EJB / CUSTOMERR");

Customerhome customerHome = (customerHome) javax.rmi.portableremoteObject.Narrow (Objhome, Customerhome.class);

Use the EJB reference to access the local interface code as follows:

Object objhome = Ic.lookup ("Java: Comp / ENV / EJB / CUSTOMERL");

CustomerLocalhome CustomerLocalHome = (CustomerLocalHome) Objhome

转载请注明原文地址:https://www.9cbs.com/read-113127.html

New Post(0)