第一个ejb,出错:javax.naming.NameNotFoundException ,Converter not bound? 是怎么查找的?在 EJB里查找 什么 Bean .贴一段查找代码出来。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 测试的代码如下: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //TODO Method stub generated by Lomboz response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html><head><title>staeless session bean</title></head><body>"); try{ InitialContext ctx = new InitialContext(); Object obj = ctx.lookup("Converter"); ConverterHome home =(ConverterHome) PortableRemoteObject.narrow (obj,ConverterHome.class); Converter bean =home.create(); out.println(bean.toLowerCase("BIG TO SAMLL")); out.println(bean.toUpperCase("small to big")); }catch(NamingException ne){ out.println("Naming Exception:"+ne); ne.printStackTrace(); }catch(RemoteException re){ out.println("Remote Exception:"+re); re.printStackTrace(); }catch(CreateException ce){ out.println("Create Exception:"+ce); ce.printStackTrace(); } out.println("</body></html>"); } 在 web.xml 中配置对 Ejb 的引用,然后把 Object obj = ctx.lookup("Converter"); 改成你 ejb-ref 中的那个名字(不是 EJB 绑定的JNDI).Object obj = ctx.lookup("java:comp/env/${你资源引用绑定的名字,不是EJB本身绑定的名字}");应用服务器相应的 扩展配置文件 xxx-ejb-jar.xml 中也有相应的配置,让这个引用对应到一个实际的JNDI 绑定 。---------------------------------------------------------------------------J2EE 1.3 开始 JNDI 绑定到相应的组建环境中 也就是 java:comp/env 的由来, EJB 真实的绑定到了 JNDI 中 ,但是你必须通过 资源引用来访问,也就是要 通过 Application Client , Web Context 或者 EJB Context 中的资源引用来访问,如果你没有处于其中的一种 环境中,就不能直接访问 EJB.需要 InitialContext 中提供相应的 Context , 配置在 web.xml 中 或 ejb-jar.xml 中的引用也行。 错了,你是在 Web 中配置 就是 相应的 xxx-web-xxx.xml ,你看看 /WEB-INF/哪个文件名是 JBoss 的配置。WebSphere 中是 ibm-web-bnd.xml ,其他的我不知道。下面这个说的 EJB 中的资源引用,WebSphere 中是 ibm-ejb-jar-bnd.xml . WebSphere 通过配置文件添加一个 id 属性,便于 IDE 自动找到并更新它的应用服务器相应的扩展配置中的条目。应用服务器相应的 扩展配置文件 xxx-ejb-jar-xxx.xml 中也有相应的配置,让这个引用对应到一个实际的JNDI 绑定 。 我的home 接口中已经定义了常量:JNDI_NAME =“Converter"(就是我的 bean的Remote 接口的名称)在ejb-jar.xml中也定义的是这个名称啊,为什么要用 java:comp/env (home 接口也定义了,eclipse自动生成的)。web-inf中好像没有你说的那个文件啊? 没有jboss的web文件,我自己建一个吧。现在明白了一点,谢谢humanity兄了! ConverterHome home =(ConverterHome) PortableRemoteObject.narrow (obj,ConverterHome.class);是这个吗?楼上的能说详细点吗 我在看<<J2EE编程技术>>这本书时,有个例子跟你这个几乎一样,可是我按照他上面的流程配置下来,可以正确运行阿 原书本行 Object obj = ctx.lookup("ConverterBean");应该为Object obj = ctx.lookup("Converter");否则也会出现你的同样的问题 好几天没来了。楼上的,我用的就是这本书的例子,但是现在却出了这样的错误:java.lang.ClassCastException com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:229) javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137) com.ejbs.TestConverter.doGet(TestConverter.java:37) javax.servlet.http.HttpServlet.service(HttpServlet.java:697) javax.servlet.http.HttpServlet.service(HttpServlet.java:810) org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)我的第37行是:ConverterHome home =(ConverterHome)PortableRemoteObject.narrow(obj,ConverterHome.class);看来是 yeshucheng兄看出来了。但是我也是按那个流程来的啊,可是不知道怎么回事,就是不能用web测试。这个怎么会是类型不对呢? 一定是你JNDI的名字写错了,把你的xxx-ejb-jar-xxx.xml配置文件贴出来看看~~ 数据库轮询的问题 jsp:incloud导入的JSP页面出现异常 tomcat5.5的admin问题 关于页面跳转 怎么使用MyEclipse的Hibernate reverse Engineering生成one-to-one映射? 如何写一个修改密码的程序啊?? 请高手介绍几本hibernet相关的书下栽 Java 高手请进。关于一个 接口类的问题。 tomcat连接池问题 java和xml问题! 部署一个ejb的jar只能提供一个ejb类的相关接口吗? 一个JAVA程序员的迷茫,献出一份爱心。欢迎大家来讨论
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//TODO Method stub generated by Lomboz
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>staeless session bean</title></head><body>");
try{
InitialContext ctx = new InitialContext();
Object obj = ctx.lookup("Converter");
ConverterHome home =(ConverterHome) PortableRemoteObject.narrow (obj,ConverterHome.class);
Converter bean =home.create();
out.println(bean.toLowerCase("BIG TO SAMLL"));
out.println(bean.toUpperCase("small to big"));
}catch(NamingException ne){
out.println("Naming Exception:"+ne);
ne.printStackTrace();
}catch(RemoteException re){
out.println("Remote Exception:"+re);
re.printStackTrace();
}catch(CreateException ce){
out.println("Create Exception:"+ce);
ce.printStackTrace();
}
out.println("</body></html>");
}
Object obj = ctx.lookup("Converter");
改成你 ejb-ref 中的那个名字(不是 EJB 绑定的JNDI).
Object obj = ctx.lookup("java:comp/env/${你资源引用绑定的名字,不是EJB本身绑定的名字}");应用服务器相应的 扩展配置文件 xxx-ejb-jar.xml 中也有相应的配置,让这个引用对应到一个实际的JNDI 绑定 。
---------------------------------------------------------------------------
J2EE 1.3 开始 JNDI 绑定到相应的组建环境中 也就是 java:comp/env 的由来,
EJB 真实的绑定到了 JNDI 中 ,但是你必须通过 资源引用来访问,
也就是要 通过 Application Client , Web Context 或者 EJB Context 中的资源引用来访问,
如果你没有处于其中的一种 环境中,就不能直接访问 EJB.
需要 InitialContext 中提供相应的 Context , 配置在 web.xml 中 或 ejb-jar.xml 中的引用也行。
是这个吗?楼上的能说详细点吗
java.lang.ClassCastException
com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:229)
javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
com.ejbs.TestConverter.doGet(TestConverter.java:37)
javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)我的第37行是:ConverterHome home =(ConverterHome)PortableRemoteObject.narrow(obj,ConverterHome.class);
看来是 yeshucheng兄看出来了。但是我也是按那个流程来的啊,可是不知道怎么回事,就是不能用web测试。这个怎么会是类型不对呢?