This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Servlet accessing Session Bean (sic)" Watch "Servlet accessing Session Bean (sic)" New topic

Servlet accessing Session Bean (sic)

Alex Nunez

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

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;


Context initial = createInitialContext();
Object objRef = initial.lookup("ejb/Stock");

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

myStock = home.create();


}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://");

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

Joined: May 26, 2003
Posts: 30130

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

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:$1
(looks ok?)

And the web.xml fragment:


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

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

Joined: May 26, 2003
Posts: 30130

Ah! Thanks for posting the solution for others who read this in the future.
I agree. Here's the link:
subject: Servlet accessing Session Bean (sic)
Similar Threads
NameNotFoundException with SUN's Application Server 8.1
NoInitialContextException thrown
EJB and Sun One Application server
com/ibm/CORBA/iiop/ORB - Help
can call EJB from Java App But not from Servlet