An EJB will always reside inside of an EJB. Normally, this container is part of the server. There are times when it is desirable for the EJB to gain access to this container. This access is provided through an instance of the EJBContext
interface, which represents the container holding the current bean. The object provides methods to access various aspects of the container including:
In this recipe we will obtain a SessionContext
object for the Salutation
EJB developed in the Creating a Simple Session Bean recipe.
The Salutation
bean developed in the first recipe will be modified. First, we start to modify the bean by adding a SessionContext
object. The easiest way of obtaining a SessionContext
object is to use dependency injection. In order to inject a SessionContext
object we will need to use the @Resource annotation. Both the annotation and the SessionContext
declaration require imports.
import javax.annotation.Resource; import javax.ejb.SessionContext;
Next we add the @Resource annotation and declare our SessionContext
variable context
as a field of the class.
@Stateless public class Salutation { ... @Resource private SessionContext context; ...
Next, create a getContextInformation
method returning a string. We can use the StringBuilder
class to build a string containing context information. There are several SessionContext
methods available. The result of these methods is appended to contextInformation
variable.
public String getContextInformation() { StringBuilder contextInformation = new StringBuilder(); contextInformation.append(context.toString() + "<br/>"); try { contextInformation.append( context.getInvokedBusinessInterface().toString() + "<br/>"); } catch (IllegalStateException e) { contextInformation.append(e); } return contextInformation.toString(); }
The simplest way to demonstrate the use of the method is to modify the SalutationServlet
. Add the getContextInformation
method call after the statement where the getFormalGreeting
method is used.
out.println("<h1>" + salutation.getFormalSalutation("Sherlock Holmes") + "</h1>"); out.println("<h2>" + salutation.getContextInformation() + "</h2>");
Execute the servlet using the URL http://localhost:8080/SalutationApplication-war/SalutationServlet
as illustrated in the following screenshot:
The stateless session bean, as we used it, limits the amount of useful context information available. In the example, we only displayed a string to represent the SessionContext
object and the business interface used by the client, neither of which are terribly exciting. Since the Salutation EJB used a no-interface view, we get back the class name. An EJB not implementing an interface is said to have a no-interface view.
The EJBContext
is the super class for SessionContext
. It possesses methods common to the three sub-interfaces of the EBJContext
interface:
Always choose the corresponding context interface for the EJB or entity in use. The EJBContext
can be obtained using either dependency inject or JNDI. We have already seen how dependency injection can be used. If we need to use JNDI instead, we need to follow the standard JNDI look up process:
lookup
method to locate the objectpublic String getContextInformationJNDI() { SessionContext sctxLookup; try { InitialContext ic = new InitialContext(); sctxLookup = (SessionContext) ic.lookup("java:comp/EJBContext"); } catch (NamingException ex) { return "NamingException: " + ex.toString(); } return sctxLookup.toString() + "<br/>" + sctxLookup.getInvokedBusinessInterface().toString() + "<br/>"; }
Notice the structure of the JNDI look up string, java:comp/EJBContext
. It is different from the previous JNDI names we have seen. The java:comp
prefix is used to allow applications to expose their components. In this case, it specifies the standard name for a SessionContext
.
18.191.157.197