/** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String user = request.getParameter("user"); String password = request.getParameter("pasw"); String mensaje; String pagina = "/Index.jsp"; HttpSession session = request.getSession(); /*ServletContext contexto = session.getServletContext();*/ try { listaUsuarios = dao.findAll(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } Usuario u = comprobarUsuario(user, password); if (u != null) { pagina = "/Perfil.jsp"; request.setAttribute("usuarios", listaUsuarios); request.setAttribute("usuario", u); /*request.setAttribute("acceso", "ok");*/ session.setAttribute("usuario", u); } else { mensaje = "Usuario o contraseña incorrectos"; request.setAttribute("mensaje", mensaje); pagina = "/Index.jsp"; } config2.getServletContext().getRequestDispatcher(pagina).forward(request, response); }
/** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub HttpSession session = request.getSession(); try { listaUsuarios = dao.findAll(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } String accion = request.getParameter("accion"); switch (accion) { case "Index": config2.getServletContext().getRequestDispatcher("/Index.jsp").forward(request, response); break; case "Salir": session.invalidate(); config2.getServletContext().getRequestDispatcher("/Index.jsp").forward(request, response); break; case "Perfil": if (session.getAttribute("usuario") != null) { try { listaUsuarios = dao.findAll(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } request.setAttribute("usuarios", listaUsuarios); request.setAttribute("usuario", session.getAttribute("usuario")); config2 .getServletContext() .getRequestDispatcher("/Perfil.jsp") .forward(request, response); } else { config2.getServletContext().getRequestDispatcher("/Index.jsp").forward(request, response); } break; case "Perfil2": request.getAttribute("usuario"); config2.getServletContext().getRequestDispatcher("/Perfil.jsp").forward(request, response); break; case "Perfiles": request.setAttribute("usuarios", listaUsuarios); config2 .getServletContext() .getRequestDispatcher("/MuestraUsuarios.jsp") .forward(request, response); break; } /*this.getServletContext().getRequestDispatcher("/Index.jsp").forward(request,response);*/ }
private Usuario comprobarUsuario(String user, String password) { Usuario usuario = dao.findByNickAndPassword(user, password); return usuario; }