1 Reply Latest reply on May 2, 2006 4:18 AM by Dragan Mijailovic

    Dependency injection and Stateful EJB

    Dragan Mijailovic Newbie

      I am the begginer in EJB 3.0 and I need help.
      I don't know what is wrong. I have my stateful EJB "UserSession" that is container for other stateful EJBs. In loginResolver.jsp page I have my UserSession EJB lookup code.

      1.
      First, I used dependency injection by using @ejb annotations to inject the other EJBs (service objects) that all are statefull too. But I can't get the EJBs statefull to store their values!

      2.
      Then I removed @ejb annotations and I called the set methods of the stateful EJBs in the constructor of my stateful EJB container "UserSession". But nothing!

      I see ( by using the debuger and by using hashcode() method of LoginMachine object) that every time when I call getLoginMachine() of the UserSession object I get the stub object that refers to new instance of the statefull LoginMachine. I don't want that!!!

      That is the 2. case. I will show the 1. case too if that is needed.

      Thanks!

      loginResolver.jsp
      <%@ page language="java"
      import="com.site.beans.ejb.interfaces.UserSession,
      javax.naming.Context,
      javax.naming.InitialContext"
      contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%>
      <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      LoginResolver



      <%
      UserSession userSession=null;
      try{
      userSession=(UserSession)session.getAttribute("userSession");
      Context ctx=null;
      if (userSession==null){
      ctx=new InitialContext();
      userSession=(UserSession)ctx.lookup("LoginJSP/UserSession");
      session.setAttribute("userSession",userSession);
      }
      }
      catch(Exception e){
      e.printStackTrace();
      }
      %>
      <%
      String sUsername=(String)request.getParameter("username");
      String sPassword=(String)request.getParameter("password");

      if (userSession.getLoginMachine().isUser(sUsername,sPassword)==true){
      %>
      <c:redirect url="welcome.jsp"></c:redirect>
      <%
      }
      else if (userSession.getLoginMachine().isAllowedNewAttempt()==true){
      userSession.setBackLink("login.jsp");
      userSession.setErrorMessage("USERNAME OR PASSWORD ARE WRONG. TRY AGAIN!");
      %>
      <c:redirect url="errorPage.jsp"></c:redirect>
      <%
      }
      else if (userSession.getLoginMachine().isAllowedNewAttempt()==false){
      userSession.setBackLink("");
      userSession.setErrorMessage("YOU ARE NOT LOGIN! YOU HAVE NOT ANY ATTEMPT TO LOGIN! ");

      %>
      <c:redirect url="errorPage.jsp"></c:redirect>
      <%
      }
      %>



      UserSession.java (the 2. case)
      package com.site.beans.ejb;

      import javax.ejb.Remote;
      import javax.ejb.Stateful;
      import javax.naming.Context;
      import javax.naming.InitialContext;

      import org.jboss.annotation.ejb.RemoteBinding;

      import com.site.beans.ejb.interfaces.LoginMachine;
      import com.site.beans.ejb.interfaces.User;
      import com.site.beans.ejb.interfaces.UserSession;

      @Remote({UserSession.class})
      @RemoteBinding (jndiBinding="LoginJSP/UserSession")
      @Stateful
      public class UserSessionBean implements UserSession {

      Context ctx=null;
      private User user;
      private LoginMachine loginMachine;
      private String errorMessage;
      private String backLink;
      public UserSessionBean(){
      System.out.println("USERSESSION CONSTRUCTOR STARTED");
      try{
      ctx=new InitialContext();
      LoginMachine loginMachine=(LoginMachine)ctx.lookup("LoginJSP/LoginMachine");
      this.setLoginMachine(loginMachine);
      User user=(User)ctx.lookup("LoginJSP/User");
      this.setUser(user);

      }
      catch(Exception e){
      e.printStackTrace();
      }
      }
      public String getBackLink() {
      return backLink;
      }
      public void setBackLink(String backLink) {
      this.backLink = backLink;
      }
      public String getErrorMessage() {
      return errorMessage;
      }
      public void setErrorMessage(String errorMessage) {
      this.errorMessage = errorMessage;
      }
      public LoginMachine getLoginMachine() {
      return loginMachine;
      }
      public void setLoginMachine(LoginMachine loginMachine) {
      this.loginMachine = loginMachine;
      }
      public User getUser() {
      return user;
      }
      public void setUser(User user) {
      this.user = user;
      }

      }

      LoginMachineBean.java
      package com.site.beans.ejb;

      import javax.ejb.Stateful;
      import org.jboss.annotation.ejb.RemoteBinding;
      import com.site.beans.ejb.interfaces.LoginMachine;
      @Stateful
      @RemoteBinding(jndiBinding="LoginJSP/LoginMachine")
      public class LoginMachineBean implements LoginMachine {
      private static final String USERNAME="admin";
      private static final String PASSWORD="admin";
      private static final int MAX_ATTEMPT=3;
      private int attemptCount=0;
      public LoginMachineBean(){
      System.out.println("LOGINMACHINEBEAN CONSTRUCTOR STARTED");
      }
      public boolean isUser(String username,String password){
      this.attemptCount++;
      if (username.equals(USERNAME)==true &&
      password.equals(PASSWORD)==true)
      return true;
      else
      return false;

      }
      public boolean isAllowedNewAttempt(){
      if (this.attemptCount>=MAX_ATTEMPT)
      return false;
      else
      return true;
      }
      public int getAttemptCount() {
      return attemptCount;
      }
      public void setAttemptCount(int attemptCount) {
      this.attemptCount = attemptCount;
      }
      }

      UserBean.java
      package com.site.beans.ejb;

      import javax.ejb.Stateful;

      import org.jboss.annotation.ejb.RemoteBinding;

      import com.site.beans.ejb.interfaces.User;

      @Stateful
      @RemoteBinding(jndiBinding="LoginJSP/User")
      public class UserBean implements User {
      private String username;
      private String password;
      public UserBean(){
      System.out.println("USERBEAN CONSTRUCTOR STARTED");
      }
      public String getPassword() {
      return password;
      }
      public void setPassword(String password) {
      this.password = password;
      }
      public String getUsername() {
      return username;
      }
      public void setUsername(String username) {
      this.username = username;
      }
      }