Skip navigation

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

13 Comments

  1. 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)

  2. 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.

  3. 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:
      EJB Modules

  4. 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”

  5. 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”

  6. Really kemal thanks for your information.
    I Havent try something like that. It will be a good information for other..

    Thanks,

  7. the post was useful.. I had tried calling ejb from jsp page using @EJB but
    it didnt work so i used JNDI and it worked ; cheers🙂

  8. Hi all, thanks for this post. However, I want to ask for the performans problem about jndi lookup and is there a way to avoid doing lookup’s in order to call ejb’s from managed bean’s?
    Even if I develop a project including myproject-web and myproject-ejb modules, I wanna believe that I should call ejb session’s in ejb module, from managed beans in web module. But I don’t know how it could be achieved. Also even if they are in different modules, they are in the same application server as well. So, I suppose I can call a bean from ejb module when I click a button.
    Again, if you have an idea about it, any help would be appreciated.

    Thanks a lot,
    Best wishes.

    • Hi thanks for your reply.
      Acctually i dont use EJB 3 much. So maybe other can help you.

      Thanks,

  9. bagus ni cing

  10. I am using glassfish v2.1 like our friend gardiary is but calling remote session beans. I keep getting the same Unresolved EJB JNDI exception.

    I didn’t have any problem deploying the app in my local system under same JVM and same glassfish server. I took it to a remote server but then deployed under single JVM and single glassfish server same as in local system. Now it gives me the error above.

    The web app is in JSF and the problem is when a managed bean tries to instantiate a session bean (remote) using @EJB, it cannot resolve the JNDI name. How is this possible under same app server?

    • Pierluigi Vernetto
    • Posted January 17, 2016 at 11:25 pm
    • Permalink
    • Reply

    with JEE 6, you can annotate your EJB with https://docs.oracle.com/javaee/6/api/javax/ejb/LocalBean.html LocalBean (no interface view) and your @EJB will work – as long as you deploy JSP and EJB in the same EAR,


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: