1, the client program calls EJB premise: EJB To implement the code of the Remote Interface client call, you can generate with the EJB Test Client tool. Write your own like this: Properties properties = new Properties (); properties.put (Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); properties.put (Context.PROVIDER_URL, "t3: // localhost: 7001");
Context context = new initialContext (Properties); Object Ref = context.lookup ("DigestSessionBean"); // Find the EJBHOME object through the JNDI Name through EJB
DigestSessionHome digestSessionHome = (DigestSessionHome) PortableRemoteObject.narrow (ref, DigestSessionHome.class); // get EJBHome DigestSession digestSession = digestSessionHome.create (); // get EJBObject
BYTE [] RET = DigestSession.md5 (Temp.getBytes ()); // EJB method call
2, the servlet is called EJB premise: The called EJB implements the Remote Interface in the servlet, the code called the code should be this: try {context context = new initialContext (); object ref = context.lookup ("userfacade);" UserFacade "; // look up jndi name and cast to Home interface UserFacadeHome userFacadeHome = (UserFacadeHome) PortableRemoteObject narrow (ref, UserFacadeHome.class);. UserFacade userFacade = userFacadeHome.create (); userFacade.updateUser ( "002", "second") } Catch (exception ex) {ex.printStackTrace ();} The difference between the EJB in the client program is on the concatenation of Context, and the servlet uses Context context = new initialContext (); and the client program is used in the client program Properties properties = new Properties (); properties.put (Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); properties.put (Context.PROVIDER_URL, "t3: // localhost: 7001");
Context context = new initialContext (property);
3, other EJB (same EJB module) premise: (1) The caller implemented the local interface, the caller implemented the Remote Interface (2) The caller and the caller should package the file in the same EJB module ( JAR) The deployment description of the caller (EJB-jar.xml) has a description of Local Ref, as shown below:
Import javax.ejb. *; import java.util.properties; import javax.naming.context; import javax.naming.initialcontext; import java.rmi.RemoteException;
Public Class UserfacadeBean Implements SessionBean {sessionContext sessionContext; private usrhome userhome; private static context context;
Public void ejbcreate () THROWS CREATEEXCEPTION {}
Public void ejbremove () {}
Public void ejbactivate () {}
Public void ejbpassiVate () {}
public void setSessionContext (SessionContext sessionContext) {System.out.println ( "@@@@@@@@@@@@@@@@ UserFacadeBean.setSessionContext ()"); this.sessionContext = sessionContext; try {findUserHome () } Catch (eXception e) {throw new ejbexception (E.GetMessage ());}} void finduserHome () throws exception {final string entity_name = "java: comp / env / ejb / user";
Context = new initialContext ();
IF (userHOME == null) {Try {Object Object = context.lookup (entity_name); userhome = (userhome) Object;} catch (exception e) {throw new ejbexception (E.getMessage ());}}}
public void addUser (String id, String name) throws RemoteException {try {User user = userHome.create (id); user.setName (name);} catch (Exception ex) {throw new RemoteException (ex.getMessage ()); }}}
4, other EJB (different EJB modules) in EJB (different EJB modules) Prerequisites: The most simple method of the Remote interface is to adjust the EJB method in the client program (or servlet).