EJB + JSP: Cannot create the Remote Object from the Home interface
I'm a newbie in the J2EE/EJB field and I'm following some basic examples. I have implemented a very simple class that sums 2 number. I have the home and remote interfaces and the Enterprise Bean (stateless) itself.
Adder.java //Remote interface
AdderHome.java //Home interface
AdderBean.java //The EJB class
When testing it as an EJB application, everything works perfectly. But now, I'd like to test it as a JSP page but I get an error (actually there's no output) at the point in which the remote interface is instantiated from the home interface.
In time: I'm using J2EE 1.4 with JBoss 4.
Here's the code of the JSP page. It's supposed to only print out one line.
<%@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 prope开发者_如何学JAVArties = 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());
}
%>
Do I need to add some new value to my web.xml?
I agree with Duffymo. What you're doing is Just Wrong.
EJB is a great technology, but EJB2 was downright evil. It was designed in an ivory tower with only a single goal: make life of developers as difficult as possible.
In this time and day there is no reason to start with EJB2. The only reason for learning EJB2 is when you have to maintain some old legacy app written with EJB2 (but then I suggest to find another job).
Despite only 1 version higher, EJB3 is a completely different thing. It's easy, lightweight, very logical and came to being with a lot of community feedback. But even EJB3 is quite old. The current version is EJB3.1, which can be used with Glassfish v3.1, JBoss AS 6 or Resin 4 (with Resin you'll get EJB3.1 lite+).
I would not recommend going this way. Scriptlets in JSPs are frowned upon.
Which Java EE app server are you using? I hope it's Java EE 5 or higher. I wonder if you're starting with a older EJB standard.
精彩评论