wood burning stoves*
The moose likes EJB and other Java EE Technologies and the fly likes Servlet accessing Session Bean (sic) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Servlet accessing Session Bean (sic)" Watch "Servlet accessing Session Bean (sic)" New topic
Author

Servlet accessing Session Bean (sic)

Alex Nunez
Greenhorn

Joined: Mar 01, 2005
Posts: 6
Hi all!

I've got a problem with the topic above.

I've got a servlet accessing to a ssesion bean (facade). When tested with a POJO local client it works fine. Though, I've this ugly exception when trying it with a servlet executing remotely, in another app:

java.lang.ClassCastException at com.sun.corba.se.internal.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:293)


I am executing all this in Sun Java Studio Enterprise IDE, which uses Tomcat 4.1 and Sun Application Server 7.

I've got in my WEB-INF/lib/ folder the files j2ee-1.3.jar and the jar file of my bean module (package StockBeans).

This is the code for the lookup:

----------------------------------------

StockBeans.Stock myStock;
StockBeans.StockHome home;


try{

Context initial = createInitialContext();
Object objRef = initial.lookup("ejb/Stock");
System.out.println(objRef.getClass().getName());

home = (AlmacenBeans.AlmacenHome) PortableRemoteObject.narrow(objRef, StockBeans.StockHome.class);

myStock = home.create();

myStock.someMethod();

}catch (Exception e){}


private static InitialContext createInitialContext() throws NamingException {
Properties env = new Properties();

env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
env.put(Context.PROVIDER_URL, "iiop://127.0.0.1:49152");

return new InitialContext(env);
}

--------------------------------------

The problem is that the servlet does access to the EJB jar file, because if I put it away, I get a NoClassDefFoundError. Plus, the servlet does connect to the orb listener of my webapp (if I type another port I get an exception) and the server can see my deployed application (if I undeploy it... guess what).

Where's the problem then??

Thanks in advance!!!
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30324
    
150

Alex,
Welcome to JavaRanch!

I suspect that "ejb/Stock" is the JNDI name for the local interface and not the remote interface. It looks like you may have suspected this with the line:

What did this output?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Alex Nunez
Greenhorn

Joined: Mar 01, 2005
Posts: 6
Thanks for your prompt reply!

hmm... I don't think so... take a look at my web.xml (and sorry, I forget the output of my code!)...

Output of System.out: com.sun.corba.ee.internal.iiop.CDRInputStream_1_0$1
(looks ok?)

And the web.xml fragment:

<ejb-ref-name>ejb/Stock</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<home>StockBeans.StockHome</home>
<remote>StockBeans.Stock</remote>
</ejb-ref>

In the ejb jar file, Stock extends EJBObject and StockHome extends EJBHome (it's remote for sure). His JNDI Name is "ejb/Stock". May the error be produced because the JNDI name of the bean is the same as when I try to lookup? Or is it mandatory?
Alex Nunez
Greenhorn

Joined: Mar 01, 2005
Posts: 6
Oh Lord!

I solved my problem.

I was using an ejb jar created with "export ejb jar". I didn't noticed that when the module is inside an application, another option comes up: "export client ejb jar".

I feel so stupid. All this time wasted...
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30324
    
150

Alex,
Ah! Thanks for posting the solution for others who read this in the future.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet accessing Session Bean (sic)