Akash
Akash

Reputation: 5012

Java Session Bean on Lan

Hi have create a interface as

@Remote
public interface MathInter{
    public int add(int x, int y);
    public int  showResult();
}

and the class as

@Stateful(mappedName="test")
public class MathImp implements MathInter{
    public int result;
    public int showResult()
    {
        return result;
    }
    @Override
    public int add(int x, int y) {
        result = x + y;
        return x+y;
    }

}

and used it on a client.jsp

<%!
    @EJB(mappedName="abc")
    MathInter m;
%>

<%
    out.write("previous result was "+m.showResult());
    out.write("result is "+m.add((int)(Math.random()*100), (int)(Math.random()*100)));
%>

The problem is that on Lan, I have 2 computers with IP address as 192.168.1.4 and 192.168.1.2. The server begin 192.168.1.4, when I visit the client.jsp page from the server, a new object of MathImp is created, and then when I access via the other comp, the same object seems to be used

Isn't it required that a new request for a new client has a new object created?

Upvotes: 1

Views: 110

Answers (1)

Mikko Maunu
Mikko Maunu

Reputation: 42074

Shortly: one JSP -> one instance of corresponding Servlet -> EJB injected once -> same instance is shared between all requests, no matter which client.

Longer explanation:

No, it is not required that container creates new instance for each new client. But it is required that for each dependency injection (as in your case) and for each JNDI lookup container creates new instance of stateful session bean.

In your case injection happens only once. That is because container compiles JSP to Servlet and there is only one instance of that Servlet (and consequently injection of ejbs to the fields of that servlet only once) that serves all requests. That's why same instance of MathInter is shared between all requests.

If you need per client instances (as is the case with stateful session beans) you should not inject those to servlet, but just for example lookup them and store reference to the HttpSession.

Upvotes: 1

Related Questions