I've wrote stateful session bean:
@Stateful
public class SessionBean {
List<Integer> list = new ArrayList<>();
public void addItem(int s) {
list.add(s);
}
public int getItemsCount() {
return list.size();
}
}
and use it in my servlet:
@WebServlet("/add")
public class AddServlet extends HttpServlet {
@Inject
SessionBean sessionBean;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int i = sessionBean.getItemsCount();
resp.getWriter().write(i + " ");
sessionBean.addItem(i + 1);
}
}
It works as expected, list saves the state and I can use it in next request. But if I'd change @Stateful on @Stateless I expected not to store state of the bean, and get in each request clean list, but it always save the state of previous request and shows new number. So what is the difference between stateless and stateful? How to see it? As I see, they work the same. I want to see example that will show something like - here we use stateful and it saves the state, and here we change on stateless and it works differently and not save the state. Please show me the differences.