<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    Re: lookup a session bean inside a servlet
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="http://community.jboss.org/people/bertoz84">stefano bertozzi</a> in <i>JNDI and Naming</i> - <a href="http://community.jboss.org/message/603275#603275">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Here's my BookShop ( EJB project ) structure:</p><p>&#160;&#160;&#160;&#160; - Book entity bean</p><p>&#160;&#160;&#160;&#160; - Utenti entity bean ( in english: Users )</p><p>&#160;&#160;&#160;&#160; - OperazioniUtenti stateless session bean ( in english: UsersOperations )</p><p>&#160;&#160;&#160;&#160; - OperazioniLibri stateless session bean ( in english: BooksOperations )</p><p>&#160;&#160;&#160;&#160; - Carrello stateful session bean ( in english: Cart )</p><p>&#160;&#160;&#160;&#160; - AccessoUtente stateful session bean ( in english: UserAccess )</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>this is the code:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><em>package bookshop.library.sessionbean;</em></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><em>import java.util.Iterator;</em></p><p><em>import java.util.List;</em></p><p><em>import javax.ejb.Stateless;</em></p><p><em>import javax.ejb.TransactionAttribute;</em></p><p><em>import javax.ejb.TransactionAttributeType;</em></p><p><em>import javax.persistence.EntityManager;</em></p><p><em>import javax.persistence.PersistenceContext;</em></p><p><em>import bookshop.library.entitybean.*;</em></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><em>@Stateless</em></p><p><em>public class OperazioniUtenti implements OperazioniUtentiLocal{</em></p><p><em>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;@PersistenceContext</em></p><p><em>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;EntityManager em;</em></p><p>&#160; </p><p><em>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;public static final String RemoteJNDIName =&#160; OperazioniUtenti.class.getSimpleName() + "/remote";</em></p><p><em>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;public static final String LocalJNDIName =&#160; OperazioniUtenti.class.getSimpleName() + "/local";</em></p><p>&#160; </p><p>&#160; </p><p><em>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;public OperazioniUtenti() {</em></p><p><em>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;}</em></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>bla bla bla....</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>i have no problem in looking up it in my BookShop project ( inside AccessoUtente bean or in a simple testing java client )</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Now again my somethingServlet inside my Web project:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">package webinterface.servlet;</em></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">import java.io.IOException;</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">import java.io.PrintWriter;</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">import javax.naming.Context;</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">import javax.naming.InitialContext;</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">import javax.naming.NamingException;</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">import javax.servlet.RequestDispatcher;</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">import javax.servlet.Servlet;</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">import javax.servlet.ServletException;</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">import javax.servlet.http.HttpServlet;</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">import javax.servlet.http.HttpServletRequest;</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">import javax.servlet.http.HttpServletResponse;</em></p><p><strong style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;"><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">import bookshop.library.entitybean.*;</em></strong></p><p><strong style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;"><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">import bookshop.library.sessionbean.*;</em></strong></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">public class somethingServlet extends HttpServlet </em><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">implements Servlet {</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; static final long serialVersionUID = 1L;</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; static Context context;</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; static OperazioniUtenti opUser;</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; static Utenti user;</em></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; public somethingServlet() {</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; super();</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }&#160; </em></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">protected void doPost(HttpServletRequest request, HttpServletResponse response) </em><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">throws ServletException, IOException {</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; try</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; context = new InitialContext();</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;"><strong style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.out.println(OperazioniUtentiLocal.class.toString());</strong> <span style="font-size: 8pt;">// print successfully INFO&#160; [STDOUT] interface bookshop.library.sessionbean.OperazioniUtentiLocal</span></em></p><p><strong style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;"><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; opUser = (OperazioniUtenti) context.lookup("/BookShop/OperazioniUtenti/local");</em></strong></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; } catch (NamingException e) {</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; e.printStackTrace();</em></p><p><em style="font-size: 13px; font-family: 'Lucida Sans', 'Lucida Sans Unicode', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</em></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>bla bla bla...</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>( ....I switched to Local from Remote....).</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Lookup returns a null pointer because it can't find BookShop.</p><p>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:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a class="jive-link-external-small" href="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</a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a class="jive-link-external-small" href="http://webcache.googleusercontent.com/search?q=cache:aIWml4XYWCEJ:java.sun.com/j2ee/verified/packaging.html+add+jar+to+war&amp;cd=6&amp;hl=it&amp;ct=clnk&amp;gl=it&amp;lr=lang_en|lang_it&amp;source=www.google.it">http://webcache.googleusercontent.com/search?q=cache:aIWml4XYWCEJ:java.sun.com/j2ee/verified/packaging.html+add+jar+to+war&amp;cd=6&amp;hl=it&amp;ct=clnk&amp;gl=it&amp;lr=lang_en|lang_it&amp;source=www.google.it</a></p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/603275#603275">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in JNDI and Naming at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2083">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>