Exemplo n.º 1
0
 private User getUser(String login, String password) {
   PersistenceManager manager = null;
   Transaction transaction = null;
   List<User> users = null;
   try {
     manager = PMF.newInstance().getPersistenceManager();
     transaction = manager.currentTransaction();
     transaction.begin();
     Query query = manager.newQuery(User.class);
     users = (List<User>) query.execute();
     transaction.commit();
   } catch (Exception e) {
     if (transaction != null) {
       transaction.rollback();
     }
   } finally {
     manager.close();
   }
   if (users != null && !users.isEmpty()) {
     for (User u : users) {
       System.out.println(u.getLogin() + " : " + u.getPassword());
       if (login.equals(u.getLogin()) && password.equals(u.getPassword())) {
         System.out.println(u.getLogin() + " : " + u.getPassword());
         return u;
       }
     }
   }
   return null;
 }
Exemplo n.º 2
0
  @Override
  public Page execute(HttpServletRequest request, HttpServletResponse response) throws IOException {
    super.execute(request, response);

    String login = request.getParameter("login");
    String password = request.getParameter("password");

    User user = getUser(login, password);
    if (user != null) {
      page.setLongLivedCookie("userName", user.getLogin());
      page.setLongLivedCookie("password", user.getPassword());
      page.setRedirectUrl("/index.shtml");

    } else {
      List<String> problems = new ArrayList<String>();
      problems.add("Логин или пароль неверны");
      page.put("problems", problems);
      page.put("content", "login.html");
    }
    return page;
  }