LoginServlet.java 4.76 KB
Newer Older
Emanuel Lugo committed
1 2
package com.roshka.proyectofinal.login;
import java.io.IOException;
3
import java.io.PrintWriter;
4
import java.security.NoSuchAlgorithmException;
Emanuel Lugo committed
5
import jakarta.servlet.RequestDispatcher;
Emanuel Lugo committed
6 7 8 9 10 11
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import com.roshka.proyectofinal.entity.LoginBean;
12
import com.roshka.proyectofinal.login.md5JavaHash;
13 14
import jakarta.servlet.http.HttpSession;
import static java.lang.System.out;
15

Emanuel Lugo committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LoginDao loginDao = new LoginDao();
43 44
        md5JavaHash passEncrip = new md5JavaHash();
        String passwordMD5 = "";
Emanuel Lugo committed
45
        response.setContentType("text/html");
46
        PrintWriter out = response.getWriter();
Emanuel Lugo committed
47 48

        String username = request.getParameter("username");
49
        String correo = request.getParameter("correo");
Emanuel Lugo committed
50 51 52
        String password = request.getParameter("password");
        LoginBean loginBean = new LoginBean();
        loginBean.setUsername(username);
53 54 55 56 57 58 59
        try {
            passwordMD5 = passEncrip.getHashPass(password);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        loginBean.setPassword(passwordMD5);
        loginBean.setCorreo(correo);
Emanuel Lugo committed
60

Emanuel Lugo committed
61
        //out.println("EL pass encriptado es: " +passwordMD5);
Emanuel Lugo committed
62 63 64

        if (loginDao.validate(loginBean))
        {
65
                HttpSession session = request.getSession(true); //incluir nota de sesion valida
Emanuel Lugo committed
66 67
                session.setAttribute("logon.isDone", correo);
                out.print ("Bienvenido " + correo);
Emanuel Lugo committed
68

69 70 71
                // Tratar de re-dirigir a la pagina que el usuario quiso acceder
                try {
                    String target = (String) session.getAttribute("login.target");
Emanuel Lugo committed
72 73
                    //response.sendRedirect("loginSuccess.jsp");
                    out.println(" \n Destino: " + target);
74 75
                    if (target != null)
                        response.sendRedirect(target);
Emanuel Lugo committed
76
                    //return;
77 78 79 80
                }
                catch (Exception ignored) { }

                // Si no es posible redireccionar a la pagina solicitada, llevar a la main page
Emanuel Lugo committed
81
                RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
82 83 84 85

        } else {

            //si no es un user valido - mandar error y redireccionar al inicio de sesion
Emanuel Lugo committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
            RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
            out.print("<div  br  align = \"center\" class=\"messageError\" > Credenciales incorrectas! Reintente ...  </div>");
            rd.include(request,response);

            }



    }



}


/* out.println("<script>alert('Datos de acceso Incorrectos, intente de nuevo !');</script>");
102
            out.println("<p> You may want to <a href='/login.jsp'> try again </a> </p>");
Emanuel Lugo committed
103
            out.println("<html><HEAD><title>Access Denied<title><head>");*/
Emanuel Lugo committed
104 105 106
//request.getRequestDispatcher("login.jsp").include(request, response);

            /*  out.println("<!DOCTYPE html>");
Emanuel Lugo committed
107 108 109 110 111 112 113 114 115 116
            out.println("<html> <head> <title>BootcampsLogin</title> </head>");
            out.println("<body> <div align= \"center\">");
            out.println("<h1>User Login Form</h1>");
            out.println("<form action=\"login\" method=\"post\">");
            out.println("<table align = \"center\">");
            out.println("<tr><td>Correo:</td> <td><input type=\"text\" name = \"correo\"></td></tr>");
            out.println("<tr><td>Password:</td><td><input type=\"password\" name=\"password\"></td></tr>");
            out.println("<center><tr><td><input type=\"submit\" value=\"Login\"/></td></tr></center>");
            out.println("</table></form> </div>");
            out.println("<div  br  align = \"center\" class=\"messageError\" > Credenciales incorrectas! Reintente ...  </div>");
Emanuel Lugo committed
117
            out.println("</body></html>");*/