Discussions

EJB programming & troubleshooting: Calling EJB from JSP

  1. Calling EJB from JSP (4 messages)

    <%@ page import="examples.ejb.basic.statelessSession.*" %>
    <%@ page import="java.rmi.RemoteException" %>
    <%@ page import="java.util.Properties" %>
    <%@ page import="javax.ejb.CreateException" %>
    <%@ page import="javax.ejb.RemoveException" %>
    <%@ page import="javax.naming.Context" %>
    <%@ page import="javax.naming.InitialContext" %>
    <%@ page import="javax.naming.NamingException" %>
    <%@ page import="javax.rmi.PortableRemoteObject" %>
    <%
        try {
    // Get an InitialContext
    Properties h = new Properties();
    h.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
           h.put(Context.PROVIDER_URL, "http://localhost:7001");
         Context ctx = new InitialContext(h);
    TraderHome home = (TraderHome)ctx.lookup("statelessSession.TraderHome");
    Trader trader = home.create();
    String st = trader.getName();
    out.print(st);

        } catch (Exception e)
        {
         e.printStackTrace();
        }
    %>

    Threaded Messages (4)

  2. Calling EJB from JSP[ Go to top ]

    Perhaps you should narrow the looked-up object to an interface extending EJBHome:

    MyHome myHome = <lookup from JNDI>
    myHome = (MyHome) PortableRemoteObject.narrow(myHome, MyHome.class);
  3. Calling EJB from JSP[ Go to top ]

    It would actually help if you give details of the exception or error you are receiving rather than just pasting in your code fragment !!!


    Some things to try, :-

    1. Since you are using a Properties class for the arg to InitialContext, supply the SECURITY_PROVIDER and SECURITY_CREDENTIALS fields just in case you haven't got integrated logins to your weblogic server (i.e connections as guest)


    2.Do you actually have a JNDI context setup as :-

    "statelessSession.TraderHome"

    or is this a package / class combinations ?

    The name you put in here is the name you have defined in your ejb-jar.xml file which maps to you weblogic-ejb-jar.xml file. There should be a <jndi-name> in that file. Put that name in there. Also, if you are using 'ejb/BeanName' syntax you may want to quote as :-

    "java:/comp/env/ejb/TraderHome"

    again, it depends on how you have constructed your xml files !!!!

    If its a JSP calling an EJB, I'm assuming you have setup the proper <ejb-ref> definition in your web.xml file ?


  4. Calling EJB from JSP[ Go to top ]

    Your "Context.PROVIDER_URL" should have a value of t3://localhost:7001 instead of http://localhost:7001
    Similarly you use "t3" in weblogic to lookup any JNDI Resources.

    ...........
    h.put(Context.PROVIDER_URL, "t3://localhost:7001");
    ..............
  5. Calling EJB from JSP[ Go to top ]

    Hello All,

    Thanks a lot for ur valuable replies.

    regards,
    Rao.