cancel
Showing results for 
Search instead for 
Did you mean: 

EJB JNDI Lookup String?

Former Member
0 Kudos

Hi,

just a short question, I try to lookup an EJB from JNDI. I found some JNDI in the documentation of how to use EJB from JSP.

Unfortunately the documentation just says:


try {

  InitialContext ic = new InitialContext();

  HelloWorldBean h= (HelloWorldBean)ic.lookup("java:comp/env/<HelloWorldBean_Lookup_String>");

  out.println(h.sayHello());

}

catch...

I'm pretty sure "<HelloWorldBean_Lookup_String>" is not the correct lookup string

Which one is it?

Frank

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Frank,

The example you mention shows how to look up the EJB declared in a servlet from jsp. So in this particular case the lookup string would be <fully-qualified name of declaring class>/<field-name>(in the example - hello.HelloWorldServlet/helloWorldBean)

If you'd like to use another lookup name, you may take advantage of @EJB name attribute:

EJB (Java EE 6 )

E.g.

@EJB (name= "test")

private HelloWorldBean helloWorldBean

and then lookup it via HelloWorldBean h= (HelloWorldBean)ic.lookup("java:comp/env/test");

Hope that helps,

Katya

Message was edited by: Vladimir Pavlov

Former Member
0 Kudos

Sorry but what you write is against everything I know about JEE Platform. The example certainly wants to look up the EJB in the JSP without usage of the Servlet. It shows how to do the lookup in the servlet with @EJB injection and in the JSP with manual lookup.

I tried to use @EJB annotation but as I'm not in a managed class here I have to do the lookup manually.

Former Member
0 Kudos

If you don't need to use the ejb injected in the servlet (and don't need the servlet at all), the lookup name should be one of the following:

"java:module/HelloWorldBean"

"java:app/HelloWorld/HelloWorldBean"

"java:global/HelloWorld/HelloWorldBean"

(According to Ejb 3.1 spec, Chapter 4.4 Global JNDI Access)

We'll fix the documentation to mention the exact namespace and bean name in other to eliminate further confusion. If you have further comments on the content please let me know.

Vlado
Advisor
Advisor
0 Kudos

First of all, it's not JEE! (Unless you mean what seems to be the Indian version of the SAT.)

Katya is absolutely right. In order to lookup something in a component environment (java:comp/env), there should be declared a reference to it - either via annotation and/or deployment descriptor entry. You can check e.g. Chapter 16 of the EJB 3.1 spec, which describes (among other things) naming rules and in particular default names.

Alternatively, you can use the global/app/module namespaces introduced in Java EE 6 (also explained by Katya above).

Cheers,

--Vlado (SCN Moderator and Java EE EG member)

Former Member
0 Kudos

What I did now is to add the following into my web.xml:

<ejb-local-ref>

  <ejb-ref-name>ejb/MyBean</ejb-ref-name>

  <local>package.MyBean</local>

</ejb-local-ref>

and the lookup through:

InitialContext ic = new InitialContext();

Mybean bean = (MyBean)ic.lookup("java:comp/env/ejb/MyBean");

Thanx for all your help

Frank

Answers (0)