Esempio n. 1
0
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    HttpSession session = req.getSession();
    String exitParam = req.getParameter("exit");
    String deleteParam = req.getParameter("delete");
    String settingsParam = req.getParameter("settings");

    if ("settings".equals(settingsParam)) {
      resp.sendRedirect("/profileSettings");
      return;
    }

    if ("exit".equals(exitParam)) {
      // обнуляем куку
      Cookie[] cookies = req.getCookies();
      if (cookies != null) {
        for (Cookie cookie : cookies) {
          if (cookie.getName().equals("remember")) {
            cookie.setMaxAge(0);
            cookie.setValue(null);
            resp.addCookie(cookie);
            break;
          }
        }
      }
      session.setAttribute("user_a", null);
      resp.sendRedirect("/login");
    }

    if ("delete".equals(deleteParam)) {
      // обнуляем куку
      Cookie[] cookies = req.getCookies();
      if (cookies != null) {
        for (Cookie cookie : cookies) {
          if (cookie.getName().equals("remember")) {
            cookie.setMaxAge(0);
            cookie.setValue(null);
            resp.addCookie(cookie);
            break;
          }
        }
      }
      try {
        UserRepository.deleteUser((User) session.getAttribute("user_a"));
      } catch (SQLException e) {
        req.setAttribute("message", "Some problems with server");
        resp.sendRedirect("/profile");

        e.printStackTrace();
      }
      session.setAttribute("user_a", null);
      resp.sendRedirect("/welcome");
    }
  }
Esempio n. 2
0
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    HttpSession session = req.getSession();

    if ((session.getAttribute("user_a") == null)) {

      Cookie cookie = ServletUtilities.getCookie(req, "remember");

      if (cookie != null) {
        // достаем user'а из БД с таким же cookie
        try {
          User user = UserRepository.getUserByCookie(cookie);
          if (user != null) {
            // меняем значение cookie для безопасности
            Cookie newCookie = new Cookie("remember", SecurityService.genRndHash(12));
            newCookie.setMaxAge(60 * 60 * 48);
            UserRepository.updateUserCookie(user, newCookie);
            resp.addCookie(newCookie);
            session.setAttribute("user_a", user);
            resp.sendRedirect("/profile");
            return;
          }

        } catch (SQLException e) {
          e.printStackTrace();
        }
      }

      resp.sendRedirect("/login");
    } else {
      req.setAttribute("user", session.getAttribute("user_a"));
      getServletContext().getRequestDispatcher("/WEB-INF/views/profile.jsp").forward(req, resp);
    }
  }