I have a stateful session bean that initializes a Collections.synchronizedList
, I add products to the list and check the list, it works(all during the same session).
But when I restart the browser it doesn't show me the list.
After watching the console I have discovered that the bean is created again.
Here is the code in my Servlet
:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
carrelloLocal carrello=null;
HttpSession session = request.getSession();
//try to recover bean
String username= (String)session.getAttribute("username");
carrello = (carrelloLocal)session.getAttribute("carrello"+username);
System.out.println("try to recover bean for user "+username);
if(carrello == null){
//new instance of the bean
carrello = lookupcarrelloLocal();
session.setAttribute("carrello"+username, carrello);
System.out.println("created new cart for user "+username);
}
List<Prodotto> lista=carrello.getCarrello();
session.setAttribute("listacarrello", lista);
getServletContext().getRequestDispatcher("/visualizzaCarrello.jsp?listacarrello=listacarrello").forward(request, response);
}