javaejbsession-bean

Why use session bean at all if ultimately its the HttpSession that holds the state


I am new to EJB so please don't mind anything silly in the question.

I have a doubt that someone might be able to solve hopefully.

I have the following Stateful Bean:

@Stateful
public class SessionBean implements SessionBeanRemote {

    private int count = 0;

    @Override
    public int getCount(){
    count++;
    return count;

    }

}

And this is the client that invokes the Bean (Servlet)

@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        InitialContext ctx;
        HttpSession session = null;
        SessionBeanRemote obj;
        try {
            if (session.getAttribute("myBean") == null) {
                ctx = new InitialContext();
                obj = (SessionBeanRemote) ctx.lookup("SessionBean/remote");
                session.setAttribute("myBean", obj);
            } else {
                obj = (SessionBeanRemote) session.getAttribute("myBean");
            }

            System.out.println(obj.getCount());

        } catch (NamingException ex) {
            Logger.getLogger(TestServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

Now I was wondering if it is ultimately HttpSession that has to hold the session bean then why use EJB at all, why not just store whatever we want to in the session directly rather than having it in the session bean first and then storing that bean in the session.

And also I was wondering if lets say I change my @Stateful annotation to @Stateless and then execute the same code on the client side and store the bean in the session then also I can extract the same bean from the session then what is the difference between stateless and stateful, I know that when new lookup is done there is a chance that same stateless bean might be returned to me where as with stateful bean it is always new when we do a lookup. But is that it?

P.S. As I mentioned earlier, I am new to EJB and all the doubts are based on what I have understood from a few tutorials online and some questions on SO. I have also tried running it locally but unfortunately I am not able to deploy the application on the GlassFish because of following error "Exception while loading the app : EJB Container initialization error". I am trying to look into it.


Solution

  • I'll try to give you a simplified answer.

    Yes, you have to store reference to the SFSB somewhere, in case of web application in http session, but as Elliott wrote you may have different client types also.

    Some benefits of Session bean vs POJO:

    If your session bean relies on state, then it is more logical to hold the state in the bean rather than pass all state info in each method call.

    Your example is extremely simple, you don't use transactions, persistence, security, so there is no point of using SFSB or even EJB at all.

    SFSB are considered to be rather heavyweight, in general should rather be avoided and I'd say majority of web applications don't use them (depends on application requirements really). So you should design your services to be stateless and rather use stateless beans than stateful, then you will have better performance and easier reusability.

    So if you plan to use some of the features I provided, you may benefit from EJBs, otherwise you maybe happy with just Http session and business logic in POJOs.