Здравствуйте, помогите пожалуйста, по заданию нужно создать и закрыть соединение и DAO в SessionListener, при этом соединение надо передать в сервлет, для использования методов (insert, delete, update)
вот сам сервлет:
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);
}}
Написал класс SessionListener, в котором должен получить соедение (за правильность реализации не уверен, поэтому прошу помощи, как правильно выполнить это задание, проще говоря необходимо передать соединение ("instance" в данном случае в сервлет)
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();
}
}}