0

我有一个初始化 a的有状态会话 beanCollections.synchronizedList,我将产品添加到列表中并检查列表,它可以工作(所有在同一个会话期间)。但是当我重新启动浏览器时,它不会显示列表。

在查看控制台后,我发现再次创建了 bean。

这是我的代码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);
}
4

0 回答 0