title: EJB3.0: Using dependency injection as opposed to the session object
body: <p>We're learning EJB 3.0 in school and we're trying to determine best practice for a problem our teacher has posed. We don't use Spring yet.</p>
  
  <p>The problem is as follows:
  Create an EJB that keeps track of the time the bean was last called by the same user (call the first user John) and displays this in a web (servlet) context. If a different user (call him Bill) calls the bean it shows the last time Bill called the bean, which is different from the time when John last did.</p>
  
  <p>Now, we can do this using the session object from the servlet Request. This would assign each session an instance of the EJB and not take advantage of dependency injection. Like this (pseudocode):</p>
  
  <pre><code>doGet{
  
     timeManagerEJB = session.getattribute("localTimeEJB");
  
     if(timeManagerEJB == null) { 
  
  timeManagerEJB = InitialContext.lookup("path...");
  
  session.setAttribute("localTimeEJB", timeManagerEJB);
  
  }
  
  }
  </code></pre>
  
  <p>Is this the best way to do it or is there a function built into the EJB framework to handle multiple instances of a stateful EJB, possibly taking advantage of dependency injection?</p>