Java Reference
In-Depth Information
private EJBSample ejb;
public void doGet (HttpServletRequestreq,
HttpServletResponse res)
throws ServletException, IOException {
try(PrintWriter out = res.getWriter()) {
out.println(ejb.greet());
}
}
}
Here, the injected EJB proxy (let's just assume that it is a POJO class annotated with a
@Stateless annotation) just points to a pool of stateless instances (or a single bean in-
stance for stateful beans). There is no automatic association between the HTTP request or
HTTP session and a given EJB instance.
The opposite is true for CDI Beans, which live in well-defined scopes. For example, the
following CDI Bean lives in RequestScoped ; that is, it will be destroyed at the end of
the request:
@RequestScoped
public class Customer {
private String name;
private String surname;
public String getName(){
return name;
}
public String getSurname(){
return surname;
}
}
The preceding CDI Bean can be safely injected into our former servlet; at the end of an
HTTP session or HTTP request, all the instances associated with this scope are automatic-
ally destroyed, and thus, garbage collected:
Search WWH ::




Custom Search