Mr. Kevin
Mr. Kevin

Reputation: 347

EJB injection - JNDI lookup fails

So I'm starting to develop a Java EE Enterprise Application with Netbeans 8.2. It's about a shop, where you have a shopping cart and you have to store each client's session correctly.

In NetBeans I have: Shop-ejb and Shop-war.

Shop-ejb contains the following classes:

Then, Shop-war contains the following servlet:

I'm using GlasshFish, and when I access to http://localhost:8080/Shop-war/Servlet I get the following error on the NetBeans console:

Grave:   javax.naming.NamingException: Lookup failed for 'java:global/Shop/Shop-ejb/CartBean!cart.CartLocal' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: Shop]

I know the lookup is failing but I tried multiple things, without result. Hope you can help me.

Upvotes: 1

Views: 2016

Answers (1)

DaviM
DaviM

Reputation: 324

You need to setup an <ejb-local-ref> tag on your web.xml:

For example:

<ejb-local-ref>
        <ejb-ref-name>CartLocal</ejb-ref-name>
        <ejb-ref-type>Session</ejb-ref-type>
        <local>cat.CartLocal</local>
        <ejb-link>CartLocal</ejb-link>
</ejb-local-ref>

And access via lookup:

try {
        if (carrito == null) {
            InitialContext ic = new InitialContext();
            carrito = (CartLocal) ic.lookup("java:comp/env/CartLocal");

            request.getSession().setAttribute(CARRITO_SESSION_KEY, carrito);
        }
    } catch (NamingException ex) {
        Logger.getLogger(Servlet.class.getName()).log(Level.SEVERE, null, ex);
    }

Upvotes: 1

Related Questions