java - EJB + JSP : Cannot create the Remote Object from the Home interface

标签 java ejb rmi remoteobject

我是 J2EE/EJB 领域的新手,我正在遵循一些基本示例。我实现了一个非常简单的类,对 2 个数字求和。我有本地和远程接口(interface)以及 Enterprise Bean(无状态)本身。

Adder.java //Remote interface
AdderHome.java //Home interface
AdderBean.java //The EJB class

当将其作为 EJB 应用程序进行测试时,一切都运行良好。但现在,我想将其作为 JSP 页面进行测试,但在从主界面实例化远程界面时出现错误(实际上没有输出)。

及时:我正在将 J2EE 1.4 与 JBoss 4 结合使用。

这是 JSP 页面的代码。它应该只打印出一行。

<%@page import="javax.naming.*" %>
<%@page import="javax.rmi.PortableRemoteObject" %>
<%@page import="java.util.Properties" %>
<%@page import="com.lucasmariano.*" %>

<%
        //Preparing the properting to be put in the InitialContext
        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
        properties.put(Context.PROVIDER_URL, "localhost:1099");

        try {
            //Get an Initial Context               
            InitialContext jndiContext = new InitialContext(properties);
            System.out.println("got context");               

            //get a reference to the bean
            Object ref = jndiContext.lookup("Adder");
            System.out.println("got reference");


            //Get a reference to this to the bean's home interface
            AdderHome home = (AdderHome) PortableRemoteObject.narrow(ref, AdderHome.class);
            out.println("GETTING OBJECT <BR />");

            //create an Adder object from the Home interface

            //######  HERE IS THE PROBLEM!!!  #######
            Adder adder = home.create();
            out.println("ADDER OBJECT CREATED <BR />");
            out.println("2 + 5 = " + adder.add(2,5));

        }catch(Exception e ){
            System.out.println(e.toString());
        }
  %>

我需要向 web.xml 添加一些新值吗?

最佳答案

我同意达菲莫的观点。你所做的就是错的。

EJB 是一项伟大的技术,但 EJB2 却是彻头彻尾的邪恶。它是在象牙塔中设计的,只有一个目标:让开发人员的生活尽可能困难。

在这个时候,没有理由开始使用 EJB2。学习 EJB2 的唯一原因是当您必须维护一些用 EJB2 编写的旧遗留应用程序时(但我建议找到另一份工作)。

尽管 EJB3 只高出 1 个版本,但它是完全不同的东西。它简单、轻量、非常合乎逻辑,并得到了大量社区反馈。但即使是 EJB3 也已经相当古老了。当前版本是 EJB3.1,它可以与 Glassfish v3.1、JBoss AS 6 或 Resin 4 一起使用(使用 Resin,您将获得 EJB3.1 lite+)。

关于java - EJB + JSP : Cannot create the Remote Object from the Home interface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5204883/

相关文章:

java - NiFi bootstrap.conf 文件上出现 Unsupported java 错误

java - 为什么我在使用 context.lookup ("java:comp/env/MyBean"时收到 NameNotFoundException )

java - 将接口(interface)转换为RMI接口(interface)

java - 关于java RMI。如何使用LocateRegistry在指定端口的远程主机上获取Registry?

java - callable 是否按顺序执行?

java - 我在创建基本联系人管理器时遇到问题。

java - Kotlin 与 Java 嵌套泛型

java - 数据交换格式和 Java 序列化

java - 拦截器API : in a classic java project

java - 异步 Java RMI