每次都创建有状态会话bean

时间:2013-06-29 19:53:58

标签: java-ee ejb-3.0 stateful-session-bean

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

在观察控制台之后,我发现再次创建了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);
}

0 个答案:

没有答案