0

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);
}
lifus
  • 8,074
  • 1
  • 19
  • 24
Antonio Foglia
  • 77
  • 2
  • 10
  • 1
    If you restart the browser, you get a new session. That's expected. The session cookie doesn't survive browser restarts. – JB Nizet Jun 29 '13 at 21:25
  • 1
    Not exactly a duplicate question, but the answer to that should be helpful: http://stackoverflow.com/questions/8887140/jsf-request-scoped-bean-keeps-recreating-new-stateful-session-beans-on-every-req/8889612#8889612 You namely seem to misunderstand the meaning of "stateful" in EJBs. The solution to your concrete functional requirement for which you incorrectly thought that *this* (as in your question) is the right solution, should be solved differently. Start learning about SQL databases and cookies. – BalusC Jun 29 '13 at 21:59
  • thanks for the answers. with the lookup i create a brand new bean each time and saving it into the http session is useless... i want to know how i can access an existing bean – Antonio Foglia Jun 30 '13 at 12:25

0 Answers0