LoginServlet.java 3.6 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
                session.setAttribute("logon.isDone", correo);
67
                //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
                    //response.sendRedirect("loginSuccess.jsp");
73
                    //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
81 82
                RequestDispatcher rd = request.getRequestDispatcher("menu.html");
                rd.include(request,response);
83 84 85 86

        } else {

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

            }



    }



}