1.

Explain The Difference Between The Look Up Of "java:comp/env/ejb/mybean" And "ejb/mybean" ?

Answer»

java:comp/env/ejb/MyBean: This is a LOGICAL reference, which will be used in your code.

ejb/MyBean: This is a physical reference where an object will be mapped to in a JNDI tree.

The logical reference (or alias) java:comp/env/ejb/MyBean is the recommended approach because you cannot GUARANTEE that the physical JNDI location ejb/MyBean you specify in your code will be available. Your code will break if the physical location is changed. The deployer will not be able to modify your code. Logical REFERENCES solve this problem by binding the logical name to the physical name in the application server. The logical names will be declared in the deployment descriptors (web.xml and/or ejb-jar.xml) as follows and these will be mapped to physical JNDI locations in the application server specific deployment descriptors. To look up a JDBC resource from either Web (web.xml) or EJB (ejb-jar.xml) tier, the deployment descriptor should have the following ENTRY

<resource-ref>
<description>The DataSource</description>
<res-ref-name>jdbc/MyDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref> 

To use it: 

Context ctx = new InitialContext();

Object ref = ctx.lookup(java:comp/env/jdbc/MyDataSource); 

To look up EJBs from another EJB or a Web module, the deployment descriptor should have the following entry: 

<ejb-ref>
<description>myBean</description>
<ejb-ref-name>ejb/MyBean</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<ejb-link>Region</ejb-link>
<home>com.MyBeanHome</home>
<remote>com.MyBean</remote>
</ejb-ref> 

To use it: 

Context ctx = new InitialContext();
Object ref = ctx.lookup(java:comp/env/ejb/MyBean); 

java:comp/env/ejb/MyBean: This is a logical reference, which will be used in your code.

ejb/MyBean: This is a physical reference where an object will be mapped to in a JNDI tree.

The logical reference (or alias) java:comp/env/ejb/MyBean is the recommended approach because you cannot guarantee that the physical JNDI location ejb/MyBean you specify in your code will be available. Your code will break if the physical location is changed. The deployer will not be able to modify your code. Logical references solve this problem by binding the logical name to the physical name in the application server. The logical names will be declared in the deployment descriptors (web.xml and/or ejb-jar.xml) as follows and these will be mapped to physical JNDI locations in the application server specific deployment descriptors. To look up a JDBC resource from either Web (web.xml) or EJB (ejb-jar.xml) tier, the deployment descriptor should have the following entry: 

<resource-ref>
<description>The DataSource</description>
<res-ref-name>jdbc/MyDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref> 

To use it: 

Context ctx = new InitialContext();

Object ref = ctx.lookup(java:comp/env/jdbc/MyDataSource); 

To look up EJBs from another EJB or a Web module, the deployment descriptor should have the following entry: 

<ejb-ref>
<description>myBean</description>
<ejb-ref-name>ejb/MyBean</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<ejb-link>Region</ejb-link>
<home>com.MyBeanHome</home>
<remote>com.MyBean</remote>
</ejb-ref> 

To use it: 

Context ctx = new InitialContext();
Object ref = ctx.lookup(java:comp/env/ejb/MyBean); 



Discussion

No Comment Found