I have an EJB with a state that I use to save current user information in my application. This EJB is injected into the servlet controller and used to store the last registered user. However, the session seems to be the same for each individual client.
EJB Code Example:
Stateful @LocalBean public class CurrentUserBean { private string Username; public void setUser(String un) { Username = un; } ....
Sample servlet code:
public class MainController extends HttpServlet { @EJB private CurrentUserBean userBean; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); String name = session.getAttribute("username"); userBean.setUser(name); ......
Now that the application is deployed to my server, and I have many different people talking to the server from several different clients, the username is always set by the last registered user. In other words, it seems that the stateful session bean maintains the same state for all clients. This really confused me when I read the following quote from page 247 in the java 6 ee tutorial:
In a session with bean state, instance variables represent the session state of the unique client / bean. Since the client interacts (βnegotiations") with his bean, this state is often called an interactive state. As its name suggests, a bean session is similar to an interactive session. The bean session is not used; it can have only one client, just as an interactive session can have only one user. When a client terminates, its bean appears to end and is no longer associated with the client.
Can someone explain why this is happening, and also explain how to use stateful beans that does not support the same state for all clients?
Thanks.
source share