I am quite new to EJB. I need stateful bean in my EAR application. I have created simple stateful session bean in an ejb module:
@Stateful
public class Test {
public Test() {
}
private int i;
@PostConstruct
public void initialize() {
i = 0;
}
public int getI() {
return i++;
}
}
And I call it from servlet in a war module:
public class TestServlet extends HttpServlet {
@EJB
Test test;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(true);
out.println("<html>");
out.println("<head>");
out.println("<title></title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>" + test.getI() + "</h1>");
out.println("</body>");
out.println("</html>");
out.close();
}
...
}
When I run it, the number gets bigger with each refresh of a browser.
0, 1, 2, ...
But when I run it in another browser, the number doesn't start from 0 but continues the session from the previous browser. It behaves like singleton.
3, 4, 5, ...
Why wasn't a new instance of the bean created? I tried to put the session bean into the war module, or annotate it with SessionScoped but result is the same.
Can you help me to create a new instance of the stateful bean for each http session?