[jboss-user] [JNDI and Naming] - Re: lookup a session bean inside a servlet

stefano bertozzi do-not-reply at jboss.com
Tue May 3 14:11:07 EDT 2011


stefano bertozzi [http://community.jboss.org/people/bertoz84] created the discussion

"Re: lookup a session bean inside a servlet"

To view the discussion, visit: http://community.jboss.org/message/603275#603275

--------------------------------------------------------------
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.*;+

+ at 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://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 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
--------------------------------------------------------------

Reply to this message by going to Community
[http://community.jboss.org/message/603275#603275]

Start a new discussion in JNDI and Naming at Community
[http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2083]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20110503/6e153222/attachment-0001.html 


More information about the jboss-user mailing list