Trying to do something like session beans. But the application works unpredictably. A servlet is like this:

@WebServlet(name = "MyServlet", urlPatterns = {"/goservlet.jsp"}) public class MyServlet extends HttpServlet { @EJB private MySessionBean mySessionBean; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Устанавливаем параметр. Текущий=" + mySessionBean.getStr1()); mySessionBean.setStr1(request.getParameter("xaction")); response.sendRedirect("/zapros.jsp"); } ... ... ... 

Bean I have Stateful :

 @Stateful(name = "MySessionEJB") public class MySessionBean { private String str; ... 

It seems that all sessions should have their own MySessionBean object. But when debugging, when requests are sent from a different computer to a server for this servlet, then the same MySessionBean object is used for one session and for another session (for one and the second user). Namely, if one user changes the field of an object, then another user will experience the same changes. Correct me if I am mistaken or tell me what the error is.

    1 answer 1

    The mistake is that you think that HTTP sessions and EJB sessions have something in common. In fact, they are not required to somehow correlate. EJB bins have their own life cycle and are governed by the EJB container regardless of the web container. In particular, EJB containers have a pool of instantiations of a bin inside and often the same instance of a bin is reused so that values ​​can be kept even for stateless bins, although they are not required to do anything like that.

    • one
      Answer, please, then in what beautiful way it is possible to achieve that for each session there were own objects? Maybe some libraries and / or standards are available? - Anton Mukhin
    • If you want to bind something to the HTTP session, then you can insert a simple bin in the session as an attribute. Then in the future it can also be used from JSP via EL. - cy6erGn0m
    • I see. Apparently, in any case, it is necessary to bring in the session. Just in JSF2 such a convenient mechanism was implemented. I did not even think about the sessions. Just put @SessionScoped and for each session the object of this class is different. - Anton Mukhin