在servlet中如何加载调用EJB??郁闷! 将EJB部署到应用服务器中,然后通过其HOME INTERFACE 来调用BEAN中的方法 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 try { Properties prop = new Properties(); prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory"); prop.put(Context.PROVIDER_URL, "t3://localhost:7001"); Context ctx=new InitialContext(prop); Object objref = ctx.lookup ("Hello"); HelloHome Hellohome = (HelloHome)PortableRemoteObject.narrow(objref, HelloHome.class); Hello Hellotest = Hellohome.create(); //@todo} catch (Exception ex) { ex.printStackTrace();} itjourney(IT之旅) 已经写得很清楚了我帮他加个注释吧,try { Properties prop = new Properties(); prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory"); //配置你ejb服务器的地址和端口 prop.put(Context.PROVIDER_URL, "t3://localhost:7001"); Context ctx=new InitialContext(prop); //查找jndi名字,这里假设是Hello Object objref = ctx.lookup ("Hello"); //建立home对象,XxxxHome就是 这里假设是ejb 是Hello 远程Home接口是XxxxHome HelloHome Hellohome = (HelloHome)PortableRemoteObject.narrow(objref, HelloHome.class); //用create方法得到远程接口 Hellotest(首字母应该小写比较规范) Hello Hellotest = Hellohome.create(); //@todo把你要进行的下一步操作写在下面例如 //Hellotest.killMyMachine();} catch (Exception ex) { ex.printStackTrace();} 如果你会用一般的客户端调用EJB,那么Servlet的调用是差不多的(如果你还不会的话要好好补补功课了),区别在于如果Web容器如果和EJB容器在一个JVM里就可以用Local接口而不是Remote接口来提高性能,当然写法只有一点点区别,即是获得Home接口的不同 /* private ml.MLAdminHome getHome() throws NamingException { return (ml.MLAdminHome) getContext().lookup(ml.MLAdminHome.JNDI_NAME); } private InitialContext getContext() throws NamingException { Hashtable props = new Hashtable(); props.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099"); // This establishes the security for authorization/authentication // props.put(InitialContext.SECURITY_PRINCIPAL,"username"); // props.put(InitialContext.SECURITY_CREDENTIALS,"password"); InitialContext initialContext = new InitialContext(props); return initialContext; } public void testBean() { try { ml.MLAdmin myBean = getHome().create(); //-------------------------------------- //This is the place you make your calls. System.out.println(myBean.getName("[email protected]")); } catch (RemoteException e) { e.printStackTrace(); } catch (CreateException e) { e.printStackTrace(); } catch (NamingException e) { e.printStackTrace(); } }*/我用上面的方法在SERVLET中调用,提示有错误,说ml.MLAdminHome 这个符号不能被识别但是上面的方法在普通的客户端是可以的,各位大侠,这是为什么呀? 1 查找jndi name2 转换类型(Home)3 调用Home的方法 用try catch的时候,要加finally吗? 服务问题 如何用JavaScript改变label的值? strict servlet API: cannot call getWriter() after getOutputStream() map.entry 内部接口给我们带来了什么好处? 要怎么在类中导入自己的写的接口啊,麻烦高手帮看一下 eclipse下svn怎么修改库地址 HTML页面关于热键问题 后天去一个公司面试,请指教 请教一个包装成EJB JAR文件的问题。 Applet在jSP怎么不能显示?高分求救。。。急 在MyEclipse环境配置Hibernate
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
prop.put(Context.PROVIDER_URL, "t3://localhost:7001");
Context ctx=new InitialContext(prop);
Object objref = ctx.lookup ("Hello");
HelloHome Hellohome = (HelloHome)PortableRemoteObject.narrow(objref, HelloHome.class);
Hello Hellotest = Hellohome.create();
//@todo
} catch (Exception ex) {
ex.printStackTrace();
}
我帮他加个注释吧,try {
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
//配置你ejb服务器的地址和端口
prop.put(Context.PROVIDER_URL, "t3://localhost:7001");
Context ctx=new InitialContext(prop);
//查找jndi名字,这里假设是Hello
Object objref = ctx.lookup ("Hello");
//建立home对象,XxxxHome就是 这里假设是ejb 是Hello 远程Home接口是XxxxHome
HelloHome Hellohome = (HelloHome)PortableRemoteObject.narrow(objref, HelloHome.class);
//用create方法得到远程接口 Hellotest(首字母应该小写比较规范)
Hello Hellotest = Hellohome.create();
//@todo把你要进行的下一步操作写在下面例如
//Hellotest.killMyMachine();
} catch (Exception ex) {
ex.printStackTrace();
}
private ml.MLAdminHome getHome() throws NamingException {
return (ml.MLAdminHome) getContext().lookup(ml.MLAdminHome.JNDI_NAME);
} private InitialContext getContext() throws NamingException {
Hashtable props = new Hashtable(); props.put(InitialContext.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099"); // This establishes the security for authorization/authentication
// props.put(InitialContext.SECURITY_PRINCIPAL,"username");
// props.put(InitialContext.SECURITY_CREDENTIALS,"password"); InitialContext initialContext = new InitialContext(props);
return initialContext;
} public void testBean() { try {
ml.MLAdmin myBean = getHome().create(); //--------------------------------------
//This is the place you make your calls.
System.out.println(myBean.getName("[email protected]")); } catch (RemoteException e) {
e.printStackTrace();
} catch (CreateException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
}
}*/
我用上面的方法在SERVLET中调用,提示有错误,说ml.MLAdminHome 这个符号不能被识别
但是上面的方法在普通的客户端是可以的,各位大侠,这是为什么呀?
2 转换类型(Home)
3 调用Home的方法