Hello, help please fix SessionListener, you need to do the following: - no fields are needed in the listener; - use the session context in the listener, not the servlet.

public class MySessionListener implements HttpSessionListener { DAOFactory daoFactory = new DAOFactory(); ServletContext context = null; @Override public void sessionCreated(HttpSessionEvent event) { context = event.getSession().getServletContext(); try { context.setAttribute("connection", daoFactory.getProfileDAO()); } catch (DAOException e) { e.printStackTrace(); } } @Override public void sessionDestroyed(HttpSessionEvent event) { try { daoFactory.getProfileDAO().close(); } catch (DAOException e) { e.printStackTrace(); } }} 
  • Apparently you are asked to make the getProfileDAO() method static, and save the resulting object as a session attribute, not a servlet. - enzo
  • statics cannot be used, rewrote like this (not sure, code below) - PolkovnikJ
  • 2
    Possible duplicate question: Creating a session for the user - Roman

1 answer 1

 public class MySessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent event) { DAOFactory daoFactory = new DAOFactory(); try { event.getSession().setAttribute("connection", daoFactory.getProfileDAO()); } catch (DAOException e) { e.printStackTrace(); } } @Override public void sessionDestroyed(HttpSessionEvent event) { ProfileDAO instance = (ProfileDAO) event.getSession().getAttribute("connection"); try { instance.close(); } catch (DAOException e) { e.printStackTrace(); } } } 
  • one
    Please try to leave a little more detailed answers. - aleksandr barakin