/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ticketmanagement; import java.io.Serializable; import javax.enterprise.context.RequestScoped; import javax.faces.context.FacesContext; import javax.inject.Named; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.ServletException; import javax.servlet.http.HttpSession; import persistence.Person; /** * * @author cirstea */ @Named(value = "ticketCreationBean") @RequestScoped public class TicketCreationBean implements Serializable { private static final String STATEFUL_TEST_BEAN_KEY = "STATEFUL_TEST_BEAN_KEY"; // if we inject, we get a new instance for each request (and consequently don't have the right functionality) // @EJB // private TicketManager ticketManager; private String firstname; private String lastname; private String topic; private String issue; // ... // explicit lookup private TicketManager getStatefulBean() throws ServletException { FacesContext facesContext = FacesContext.getCurrentInstance(); HttpSession httpSession = (HttpSession) facesContext.getExternalContext().getSession(false); TicketManager ticketManager = (TicketManager) httpSession.getAttribute(STATEFUL_TEST_BEAN_KEY); System.out.println("TicketManager = " + ticketManager); if (ticketManager == null) { try { InitialContext ic = new InitialContext(); ticketManager = (TicketManager) ic.lookup("java:global/GestionPersonnes/GestionPersonnes-ejb/TicketManagerBean"); httpSession.setAttribute(STATEFUL_TEST_BEAN_KEY, ticketManager); } catch (NamingException e) { throw new ServletException(e); } } return ticketManager; } private void invalidateSessionTicket() { FacesContext facesContext = FacesContext.getCurrentInstance(); HttpSession httpSession = (HttpSession) facesContext.getExternalContext().getSession(false); httpSession.setAttribute(STATEFUL_TEST_BEAN_KEY, null); } }