Reputation: 347
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:
CartLocal.java
package cart;
@Local
public interface CartLocal{
}
CartBean.java
package cart;
@Stateful
@StatefulTimeout(unit = TimeUnit.MINUTES, value = 20)
public class CartBean implements CartLocal{
}
Then, Shop-war
contains the following servlet:
Servlet.java
import cart.CartLocal;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet(name = "Servlet", urlPatterns = {"/Servlet"})
public class Servlet extends HttpServlet {
private static final String CARRITO_SESSION_KEY = "shoppingCart";
public Servlet() {
super();
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
CartLocal carrito = (CartLocal) request.getSession().getAttribute(CARRITO_SESSION_KEY);
try {
if (carrito == null) {
InitialContext ic = new InitialContext();
carrito = (CartLocal) ic.lookup("java:global/Shop/Shop-ejb/CartBean!cart.CartLocal");
request.getSession().setAttribute(CARRITO_SESSION_KEY, carrito);
}
} catch (NamingException ex) {
Logger.getLogger(Servlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
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
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