Here's my BookShop ( EJB project ) structure:
- Book entity bean
- Utenti entity bean ( in english: Users )
- OperazioniUtenti stateless session bean ( in english: UsersOperations )
- OperazioniLibri stateless session bean ( in english: BooksOperations )
- Carrello stateful session bean ( in english: Cart )
- AccessoUtente stateful session bean ( in english: UserAccess )
this is the code:
package bookshop.library.sessionbean;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import bookshop.library.entitybean.*;
@Stateless
public class OperazioniUtenti implements OperazioniUtentiLocal{
@PersistenceContext
EntityManager em;
public static final String RemoteJNDIName = OperazioniUtenti.class.getSimpleName() + "/remote";
public static final String LocalJNDIName = OperazioniUtenti.class.getSimpleName() + "/local";
public OperazioniUtenti() {
}
bla bla bla....
i have no problem in looking up it in my BookShop project ( inside AccessoUtente bean or in a simple testing java client )
Now again my somethingServlet inside my Web project:
package webinterface.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bookshop.library.entitybean.*;
import bookshop.library.sessionbean.*;
public class somethingServlet extends HttpServlet implements Servlet {
static final long serialVersionUID = 1L;
static Context context;
static OperazioniUtenti opUser;
static Utenti user;
public somethingServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try
{
context = new InitialContext();
System.out.println(OperazioniUtentiLocal.class.toString()); // print successfully INFO [STDOUT] interface bookshop.library.sessionbean.OperazioniUtentiLocal
opUser = (OperazioniUtenti) context.lookup("/BookShop/OperazioniUtenti/local");
} catch (NamingException e) {
e.printStackTrace();
}
bla bla bla...
( ....I switched to Local from Remote....).
Lookup returns a null pointer because it can't find BookShop.
I read a lot about this problem but the best answer i found says to put my BookShop.jar inside MYWEBPROJECTPATH/WebContent/WEB_INF/lib and be sure to have it in the java build path of the web project ( that's what i did ). Here the links:
http://devpinoy.org/blogs/lamia/archive/2008/01/03/deploying-your-jar-with-your-war-in-eclipse.aspx
http://webcache.googleusercontent.com/search?q=cache:aIWml4XYWCEJ:java.sun.com/j2ee/verified/packaging.html+add+jar+to+war&cd=6&hl=it&ct=clnk&gl=it&lr=lang_en|lang_it&source=www.google.it