I use Glassfish for application server. Here is the EJB:
(interface)
package ejb3.bean; import javax.ejb.Remote; @Remote public interface HelloEjb { String printHello(String name); }
(implementation)
package ejb3.bean; import javax.ejb.Stateless; @Stateless( name="HelloEjb") public class HelloEjbBean implements HelloEjb { public HelloEjbBean() {} public String printHello(String name) { return "Hello bro " + name + " welcome to EJB 3"; } }
Package it as JAR or EAR, start Glassfish( GLASSFISH_HOME/bin/asadmin start-domain DOMAIN_NAME ) then use Admin Console (default: http://localhost:4848 ) to deploy the EJB.
To call the EJB from servlet we can directly use @EJB annotations, to get the instance of the EJB. In below sample i send the EJB to JSP page:
package ejb3.servlet; import ejb3.bean.HelloEjb; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import javax.ejb.EJB; import java.io.IOException; public class HelloEjb3Servlet extends HttpServlet { @EJB private HelloEjb helloEjb; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { execute(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { execute(request, response); } private void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute("helloEjb", helloEjb); request.setAttribute("printHello", helloEjb.printHello("Djunaedi")); request.getRequestDispatcher("/HelloEjb3.jsp").forward(request, response); } }
To get EJB instance in JSP using @EJB (inside scriplets) isn’t working, i get null instance. So i use JNDI lookup and that’s work:
<%@ page import="ejb3.bean.HelloEjb" %> <%@ page import="javax.naming.InitialContext" %> <%@ page import="javax.naming.Context" %> <html> <head><title>Hello EJB 3 !</title></head> <body> <%! HelloEjb helloEjb; %> <% try { Context context = new InitialContext(); helloEjb = (HelloEjb) context.lookup(HelloEjb.class.getName()); } catch(Exception e) { // exception code here } %> <p> Object HelloEjb Directly : <%= helloEjb %><br> Print Hello : <%= helloEjb.printHello("Ujang") %> <p> Object HeloEjb from Servlet : <%= request.getAttribute("helloEjb") %><br> Print Hello : <%= request.getAttribute("printHello") %> </body> </html>
notes:
Calling EJB 3 using JNDI lookup isn’t the same between Application Server, JNDI lookup name is not standardized. Refer to this article find out more.
References:
- http://www.adam-bien.com/roller/abien/entry/ejb_3_portability_issue_why
- http://javahowto.blogspot.com/2006/09/default-jndi-name-for-resource-or-ejb.html
- https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html

7 Comments
Hi,
i got the error below when using jndi lookup on jsp. can you help me about what am i doing wrong.
i have a local interface called RegisterService and a bean implementation called RegisterServiceBean, both in the same package, session. they are just simple as the ones in your example and i try to use jndi lookup just like the jsp example you gave above. however, i’m getting the error below.
error:
javax.naming.NameNotFoundException: session.RegisterService not found
at com.sun.enterprise.naming.TransientContext.doLookup(TransientContext.java:216)
at com.sun.enterprise.naming.TransientContext.lookup(TransientContext.java:188)
at com.sun.enterprise.naming.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:74)
at com.sun.enterprise.naming.LocalSerialContextProviderImpl.lookup(LocalSerialContextProviderImpl.java:111)
at com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:409)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
What is your application server? Calling EJB 3 using JNDI lookup isn’t the same between Application Server.
If u use glassfish, u shoudn’t get an error.
I used glassfish 2.1 but… I still got this error
descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: PWC1391: Servlet.init() for servlet HelloEjb3Servlet threw exception
root cause
java.lang.RuntimeException: WEB5002: Exception in handleBeforeEvent.
root cause
com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref ejb3.servlet.HelloEjb3Servlet/helloEjb@jndi: ejb3.bean.HelloEjb@null@ejb3.bean.HelloEjb@Session@null into class ejb3.servlet.HelloEjb3Servlet
root cause
javax.naming.NameNotFoundException: ejb3.bean.HelloEjb#ejb3.bean.HelloEjb not found
Could you pls advise me? Thanks
Have you deploy the EJB’s? It will look something like this, in your admin console:

Hi,
I’m sorry for this late reply.
I’m using Glassfish V2.1 in my project, too. And yes, it gives this exception. It also not related with deploying the EJB module, because i deploy my project as an enterprise application with all of its modules.
What i understand from searching google and my try outs is that if you use local interface you have to declare it in the web.xml file of the web module. There is no problem with the remote interfaces. The remote interfaces are automatically added to jndi.
I added something like this into my web.xml file for my locale interface:
ejb/my/jndi/path/sb
Session
com.my.app.SBInterfaceLocal
And look up the EJB with the name “java:comp/env/ejb/my/jndi/path/sb”
Sorry, i don’t know how to post xml tags. I encoded it, i wish this work:
I added something like this into my web.xml file for my locale interface:
<ejb-local-ref>
<ejb-ref-name>ejb/my/jndi/path/sb</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local-home/>
<local>com.my.app.SBInterfaceLocal</local>
</ejb-local-ref>
And look up the EJB with the name “java:comp/env/ejb/my/jndi/path/sb”
Really kemal thanks for your information.
I Havent try something like that. It will be a good information for other..
Thanks,