0

I have a ManagedBean (SecurityTokenController) that use a Stateful SessionBean (SecurityTokenClient). Each time I call the controller, a new SecurityTokenClient bean is instantiated, so I lose the state. What am I doing wrong?

Here is the code of the ManagedBean:

@ManagedBean
public class SecurityTokenController implements Serializable {
  @EJB
  SecurityTokenClient client;

  public String askToken(){
    return client.getToken().toString();
  }
}

Here there is the code of the Stateful SessionBean:

@Stateful
@SessionScoped
public class SecurityTokenClient implements Serializable {
  SecurityToken securityToken;   

  public SecurityTokenVO getToken(){
      if ( null == this.securityToken ) {
          this.securityToken = new SecurityToken();
      }
      return this.securityToken;
  }
}

So, every time I call this bean the securityToken is null. The injected EJB is always a different one. What did I do wrong?

Sean Mickey
  • 7,618
  • 2
  • 32
  • 58
Andrea T
  • 3,035
  • 4
  • 23
  • 39
  • No scope defined for stateful bean. Maybe you should add @SessionScoped? – skegg99 Jun 18 '14 at 12:10
  • Yes, I tried, but I had the same result. So I omitted it. I add again. – Andrea T Jun 18 '14 at 12:20
  • I guess your EJB stateful bean is not associated with session automatically. You can use CDI for it I belive. Some more details can be found here http://stackoverflow.com/questions/8480096/using-a-stateful-session-bean-to-track-an-users-session/8540181#8540181 – skegg99 Jun 18 '14 at 12:45
  • How are you calling the Controller? And what is calling the Controller? – Sean Mickey Jun 25 '14 at 03:57

0 Answers0