Accessing Stateful session bean from servlet/jsp throws ClassCast exception in jboss 4.2.2

ejb-3.0jboss

Whenever I try to access EJB 3 Stateful session bean deployed on JBoss 4.2.2 application server using a web client(within JBoss server) such as servlet/jsp , I get following exception
java.lang.ClassCastException: $Proxy123
If I use application client , same code works without any exception.
Or if I change bean to @Stateless , even web client works fine.
If I deploy my .war on the tomcat container externally, again web client works fine for stateful bean also.

So in short : Stateful bean EJB 3 deployed on JBoss 4.2 and accessed via web client deployed on JBoss throws classcastexception

This is my code – client side, written in a servlet

Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");

        Context c = new InitialContext(properties);
        InvokePOJORemote ref = (InvokePOJORemote) c.lookup("InvokePOJOBean/remote");
        out.println("<h1>Servlet ejb ref " + ref + "</h1>");
        boolean sts = ref.addEmployee("Mad", (short) 30, new java.math.BigDecimal(12000));

This throws ClasscastException at the lookup statement , at time of casting. Dont know what is wrong! Pls help.

Best Answer

This is a bit disgusting but you need to keep the stateful ejb interface outside the war file.That is neither in the classes folder(of WEB-INF) as .class file nor in the lib folder(of WEB-INF) as .jar file.

Instead wht u can do is create a jar file of the stateful ejb interface and for servlet compilation add the jar to ur project.. say using "add external jars" in Eclipse.

That should work..

Related Topic