javajakarta-eeejbjavabeansstateful-session-bean

Getting javax.servlet.ServletException: javax.naming.NamingException: Lookup failed for 'java:global/ShoppingCart-1.0-SNAPSHOT/ShoppingCartRemote'


I am new to JAVA EE. I am trying to implement stateful session Bean via IntelliJ IDEA and glassfish server 4.0 while running the code getting below exception. Attaching the code and glassfish configuration as well. Error is coming as the lookup has been failed. Can anyone help regarding the lookup? Glassfish Configuration

javax.servlet.ServletException: javax.naming.NamingException: Lookup failed for 'java:global/ShoppingCart-1.0-SNAPSHOT/ShoppingCartRemote' 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: ShoppingCartRemote not found]
javax.naming.NamingException: Lookup failed for 'java:global/ShoppingCart-1.0-SNAPSHOT/ShoppingCartRemote' 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: ShoppingCartRemote not found]
javax.naming.NameNotFoundException: ShoppingCartRemote not found

Remote Interface

package com.example.shoppingcart;

import javax.ejb.Remote;
import java.util.List;

@Remote
public interface ShoppingCartRemote {
    void addItem(String e);
    void removeItem(String e);
    List<String> getItems();
}

Stateful Session Bean

package com.example.shoppingcart;

import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import java.util.ArrayList;
import java.util.List;


@Stateful(mappedName = "ShoppingCart")
@LocalBean
public class ShoppingCartEJB implements ShoppingCartRemote{
    List<String> values;

    public ShoppingCartEJB(){
        values = new ArrayList<String>();
    }

    @Override
    public void addItem(String e) {
        values.add(e);
    }

    @Override
    public void removeItem(String e) {
        values.remove(e);
    }

    @Override
    public List<String> getItems() {
        return values;
    }
}

JSP Code

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="com.example.shoppingcart.ShoppingCartRemote" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="java.util.List" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
    ShoppingCartRemote values = (ShoppingCartRemote) session.getAttribute("sessionShoppingCart");
    if(values == null) {
        InitialContext ct = new InitialContext();
        ShoppingCartRemote scart = (ShoppingCartRemote) ct.lookup("java:global/ShoppingCart-1.0-SNAPSHOT/ShoppingCartEJB");
        session.setAttribute("sessionShoppingCart", scart);
    }


%>
<html>
    <head>
        <title>Shopping Cart</title>
    </head>
    <body>
        <h1><%= "Hello! Please Add the items in your cart!" %>
            <br>
            <form name="cart" method="post">
                <input type="text" value="" name="t1"> <br>
                <input type="submit" value="Add to Cart" name="addItem"><br>
                <input type="submit" value="Remove from Cart" name="remItem"><br>
                <%
                    if (values!=null){
                        List<String> items = values.getItems();
                        out.println("<br> Total Items in your Cart:: "+ items.size());
                        for (String item: items) {
                            out.println("<br>"+item);
                        }
                    }
                %>
            </form>

                <%
                    if (request.getParameter("addItem")!=null){
                        String s = request.getParameter("t1");
                        if(s.length()!=0) {
                            values.addItem(s);
                        }
                    }
                    if (request.getParameter("remItem")!=null){
                        String s = request.getParameter("t1");
                        if(s.length()!=0) {
                            values.removeItem(s);
                        }
                     }
                %>
    </body>
</html>

Solution

  • The error was due to an incorrect lookup path. The updated code in the JSP file is

    <%
        ShoppingCartRemote values = (ShoppingCartRemote)session.getAttribute("sessionShoppingCart");
        if(values == null)
        {
            InitialContext ct = new InitialContext();
            values = (ShoppingCartRemote) ct.lookup("java:global/ShoppingCart-1.0-SNAPSHOT/ShoppingCartEJB!com.example.shoppingcart.ShoppingCartRemote");
            session.setAttribute("sessionShoppingCart", values);
        }
    %>
    

    To find out the lookup path, I have checked the server logs it was printed over there, after fixing that it's working fine.