What is the diffrence between java:comp/ and java:comp/env/
Imagine both as directories on your file system. You might have files (environment entries) in both directories (contexts).
The java:comp namespace exists per component (like the "My Documents" folder exists for each user on Microsoft Windows). By default, environment entries declared by the components are created in java:comp/env. The Java EE 6 specification recommends (but not requires) that environment entries be created in the env subcontext to avoid potential conflicts with current or future versions of the specification. I.e. the future versions of the spec may require that certain entries be created in the java:comp context - as EJBContext already is.
may I know the diffrence between
In this case, if you imagine the contexts again as directories on your file system, you can imagine the entries 2 and 3 as symbolic links to java:comp/EJBContext.
Symbolic link 2 java:comp/env/com.foo.ejb.HelloBean/sctx is created because that is the default: full qualified class name (com.foo.ejb.HelloBean) + "/" + name of member variable (sctx):
Symbolic link 3 java:comp/env/sessionContext is created because the code explicitly says so in the name attribute of the Resource annotation:
Sidenote: Even though the 4 ways to get the EJBContext in EJB 3.1 are correct, I do not see why I should use JNDI lookup if I already have access via the member variable. Nevertheless, it demonstrates nicely under which entry an object is made available.
Joined: Apr 07, 2009
BTW: casting the EJBContext to a SessionContext only works because the EJBContext is accessed in an EJB session bean. If you accessed the EJBContext in a MessageDrivenBean, it would be a MessageDrivenContext; in an Entity bean it would be an EntityContext.