示例#1
0
  /**
   * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
      /* TODO output your page here. You may use following sample code. */
      String email = request.getParameter("email");
      String password = request.getParameter("password");

      HttpSession session = request.getSession(true);

      FactoryUtilizador fu = new FactoryUtilizador();
      List utilizadores = new ArrayList<Utilizador>();

      utilizadores = fu.loginAdmin(email, password);

      ArrayList<Utilizador> utilizadores1 = new ArrayList<Utilizador>();
      utilizadores1 = (ArrayList<Utilizador>) utilizadores;

      // int nivel=2;

      for (Utilizador u : utilizadores1) {
        out.println("Bem vindo: " + u.getNome() + " " + u.getApelido());

        out.println(2);
        // if(nivel==2){
        session.setAttribute("admin", u.getNivel());
        session.setAttribute("nome", u.getNome());
      }

      if (utilizadores.isEmpty()) {
        response.sendRedirect(request.getContextPath() + "/erro.jsp");
      } else {
        session.setAttribute("email", email);
        response.sendRedirect(request.getContextPath() + "/index.jsp");
      }
    }
  }