users@glassfish.java.net

About Stateless / Stateful Session Bean

From: <glassfish_at_javadesktop.org>
Date: Fri, 22 Aug 2008 07:11:13 PDT

Hey,

I wrote an example Session Bean like this:

[i]@Stateful
public class CounterBean implements CounterBeanRemote {
    private int counter;

    @Override
    public int getCounter() {
        return counter++;
    }
}[/i]

And a JSP with this code inside:

[i]
<%
Context ctx = new InitialContext();
CounterBeanRemote bean = (CounterBeanRemote)ctx.lookup( CounterBean );
out.write(Counter is + bean.getCounter() + <br>);
%>
[/i]

Its realize in an Enterprise Application Project.

I opened the webpage and I see "Counter is 0". I refreshed the page and also see "Counter is 0".

I changed the Annotation from @Stateful to @Stateless then I opened the page again and got "Counter is 0". I refresh the page and got "Counter is 1" and so on.

Why it is so? I thought @Stateful has a memory between 2 Sessions. Or its something wrong in my mind?

Can someone explain?

Regards, Kai Opalka
[Message sent by forum member 'kaiopalka' (kaiopalka)]

http://forums.java.net/jive/thread.jspa?messageID=294833