Hello, please help, on assignment you need to create and close the connection and DAO in the SessionListener, while the connection must be passed to the servlet to use the methods (insert, delete, update)
here is the servlet itself:
public class Servlet extends HttpServlet { private static String INSERT = "/profile.jsp"; private static String EDIT = "/edit.jsp"; private static String PROFILE_RECORD = "/listProfile.jsp"; protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); request.setCharacterEncoding("UTF-8"); super.service(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String redirect=""; String profileId = request.getParameter("id"); String action = request.getParameter("action"); try { DAOFactory daoFactory = new DAOFactory(); //get factory ProfileDAO instance = daoFactory.getProfileDAO(); //get connection Profile profile = new Profile(); if(!((profileId)== null) && action.equalsIgnoreCase("insert")){ int id = Integer.parseInt(profileId); profile.setId(id); profile.setUserName(request.getParameter("userName")); profile.setNickName(request.getParameter("nickName")); profile.setUserMail(request.getParameter("userMail")); profile.setPassword(request.getParameter("password")); instance.insertProfile(profile); redirect = PROFILE_RECORD; request.setAttribute("profile", instance.selectAllProfiles()); } else if (action.equalsIgnoreCase("delete")){ int profileDelete = Integer.parseInt(profileId); instance.deleteProfileId(profileDelete); redirect = PROFILE_RECORD; request.setAttribute("profile", instance.selectAllProfiles()); } else if (action.equalsIgnoreCase("editForm")){ redirect = EDIT; } else if (action.equalsIgnoreCase("edit")){ String Id = request.getParameter("id"); int profileEdit = Integer.parseInt(Id); profile.setId(profileEdit); profile.setUserName(request.getParameter("userName")); profile.setNickName(request.getParameter("nickName")); profile.setUserMail(request.getParameter("userMail")); profile.setPassword(request.getParameter("password")); instance.updateProfile(profile); request.setAttribute("profile", profile); redirect = PROFILE_RECORD; } else if (action.equalsIgnoreCase("listProfile")){ redirect = PROFILE_RECORD; request.setAttribute("profile", instance.selectAllProfiles()); } else { redirect = INSERT; } RequestDispatcher rd = request.getRequestDispatcher(redirect); rd.forward(request, response); } catch (Exception e){ e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }} I wrote a SessionListener class in which I should get a connection (I'm not sure about the correct implementation, so I ask for help on how to do this task correctly, in other words, you must transfer the connection ("instance" in this case to the servlet)
public class SessionListener implements HttpSessionListener{ ProfileDAO instance = null; ServletContext servletContext = null; private int sessionCount; public SessionListener() { this.sessionCount = 0; } @Override public void sessionCreated (HttpSessionEvent event) { synchronized (this) { sessionCount++; } DAOFactory daoFactory = new DAOFactory(); try { instance = daoFactory.getProfileDAO(); } catch (DAOException e) { e.printStackTrace(); } servletContext = event.getSession().getServletContext(); servletContext.setAttribute("instance", instance); } @Override public void sessionDestroyed(HttpSessionEvent event) { synchronized (this) { --sessionCount; } try { instance.close(); } catch (DAOException e) { e.printStackTrace(); } }}