import java.rmi.*; import java.util.*;import javax.naming.*; import javax.rmi.*;import org.apache.log4j.*;public class WorkflowCustomDAO { static final private String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first."; static final private int MAX_OUTPUT_LINE_LENGTH = 100; private WorkflowCustomDAO() {
try { //get naming context Context ctx = getInitialContext(); //look up jndi name Object ref = ctx.lookup("SessionBean"); //cast to Home interface SessionHome = (SessionHome) PortableRemoteObject.narrow(ref, SessionHome.class); create();
} catch(Exception e) {
} } private Context getInitialContext() throws Exception { String url = "t3://localhost:7001"; String user = null; String password = null; Properties properties = null; try { properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); properties.put(Context.PROVIDER_URL, url); if (user != null) { properties.put(Context.SECURITY_PRINCIPAL, user); properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password); } return new InitialContext(properties); } catch(Exception e) { log("Unable to connect to WebLogic server at " + url); log("Please make sure that the server is running."); logger.warn("调用EJB失败,具体原因是:"+e.getMessage()); throw e; } }}
import java.util.*;import javax.naming.*;
import javax.rmi.*;import org.apache.log4j.*;public class WorkflowCustomDAO {
static final private String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
static final private int MAX_OUTPUT_LINE_LENGTH = 100; private WorkflowCustomDAO() {
try {
//get naming context
Context ctx = getInitialContext(); //look up jndi name
Object ref = ctx.lookup("SessionBean"); //cast to Home interface
SessionHome = (SessionHome) PortableRemoteObject.narrow(ref, SessionHome.class);
create();
}
catch(Exception e) {
}
} private Context getInitialContext() throws Exception {
String url = "t3://localhost:7001";
String user = null;
String password = null;
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
} return new InitialContext(properties);
}
catch(Exception e) {
log("Unable to connect to WebLogic server at " + url);
log("Please make sure that the server is running.");
logger.warn("调用EJB失败,具体原因是:"+e.getMessage());
throw e;
}
}}
env.put(Context.INITIAL_CONTEXT_FACTORY,JndiKey.getContexFactory());
env.put(Context.SECURITY_PRINCIPAL, "admin");
env.put(Context.SECURITY_CREDENTIALS, "admin");
env.put(Context.PROVIDER_URL,"ormi://192.168.1.1/XXEJB");
Context ctx = new InitialContext(env);
XXEJBHome xxEJBHome = (XXEJBHome)ctx.lookup("XXEJB");
XXEJB xxEJB = xxEJBHome.create();
那个是客户端程序调用jsp如下
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="javax.naming.*,javax.rmi.PortableRemoteObject,testone.*" %><%
InitialContext context = new InitialContext();
Object obj = context.lookup("Test");
TestHome home = (TestHome)PortableRemoteObject.narrow(obj,TestHome.class);
Test test = home.create();
out.println(test.show());%>Test是那个ejb,show是方法,大致就是这样了
你仔细看看我们的区别,你这样只能调用本地EJB
这也符合mvc