Commit 1fbbca11 by Jose Baez

Merge branch 'login2' into 'develop'

Updated login

See merge request !19
parents 3483baec 965ec053
package com.roshka.proyectofinal;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.*;
public class LoginHandler extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
// Get the user's name and password
String name = req.getParameter("name");
String passwd = req.getParameter("passwd");
// Check the name and password for validity
if (!allowUser(name, passwd)) {
out.println("<HTML><HEAD><TITLE>Access Denied</TITLE></HEAD>");
out.println("<BODY>Your login and password are invalid.<BR>");
out.println("You may want to <A HREF=\"/login.html\">try again</A>");
out.println("</BODY></HTML>");
}
else {
// Valid login. Make a note in the session object.
HttpSession session = req.getSession(true);
session.putValue("logon.isDone", name); // just a marker object
// Try redirecting the client to the page he first tried to access
try {
String target = (String) session.getValue("login.target");
if (target != null)
res.sendRedirect(target);
return;
}
catch (Exception ignored) { }
// Couldn't redirect to the target. Redirect to the site's home page.
res.sendRedirect(req.getScheme() + "://" +
req.getServerName() + ":" + req.getServerPort());
}
}
protected boolean allowUser(String user, String passwd) {
return true; // trust everyone
}
}
\ No newline at end of file
package com.roshka.proyectofinal.login; package com.roshka.proyectofinal.login;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException; import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import com.roshka.proyectofinal.entity.LoginBean; import com.roshka.proyectofinal.entity.LoginBean;
import com.roshka.proyectofinal.login.md5JavaHash; import com.roshka.proyectofinal.login.md5JavaHash;
import jakarta.servlet.http.HttpSession; import jakarta.servlet.http.HttpSession;
import static java.lang.System.out; import static java.lang.System.out;
/** /**
* Servlet implementation class LoginServlet * Servlet implementation class LoginServlet
*/ */
@WebServlet("/login") @WebServlet("/login")
public class LoginServlet extends HttpServlet { public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** /**
* @see HttpServlet#HttpServlet() * @see HttpServlet#HttpServlet()
*/ */
...@@ -48,6 +42,7 @@ public class LoginServlet extends HttpServlet { ...@@ -48,6 +42,7 @@ public class LoginServlet extends HttpServlet {
LoginDao loginDao = new LoginDao(); LoginDao loginDao = new LoginDao();
md5JavaHash passEncrip = new md5JavaHash(); md5JavaHash passEncrip = new md5JavaHash();
String passwordMD5 = ""; String passwordMD5 = "";
response.setContentType("text/html");
PrintWriter out = response.getWriter(); PrintWriter out = response.getWriter();
String username = request.getParameter("username"); String username = request.getParameter("username");
...@@ -60,42 +55,43 @@ public class LoginServlet extends HttpServlet { ...@@ -60,42 +55,43 @@ public class LoginServlet extends HttpServlet {
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
e.printStackTrace(); e.printStackTrace();
} }
out.println(passwordMD5);
loginBean.setPassword(passwordMD5); loginBean.setPassword(passwordMD5);
loginBean.setCorreo(correo); loginBean.setCorreo(correo);
out.println("EL pass encriptado es: " +passwordMD5); //out.println("EL pass encriptado es: " +passwordMD5);
if (loginDao.validate(loginBean)) if (loginDao.validate(loginBean))
{ {
HttpSession session = request.getSession(true); //incluir nota de sesion valida HttpSession session = request.getSession(true); //incluir nota de sesion valida
session.setAttribute("logon.isDone", username); session.setAttribute("logon.isDone", correo);
//out.print ("Bienvenido " + correo);
// Tratar de re-dirigir a la pagina que el usuario quiso acceder // Tratar de re-dirigir a la pagina que el usuario quiso acceder
try { try {
String target = (String) session.getAttribute("login.target"); String target = (String) session.getAttribute("login.target");
response.sendRedirect("loginSuccess.jsp"); //response.sendRedirect("loginSuccess.jsp");
//out.println(" \n Destino: " + target);
if (target != null) if (target != null)
response.sendRedirect(target); response.sendRedirect(target);
return; //return;
} }
catch (Exception ignored) { } catch (Exception ignored) { }
// Si no es posible redireccionar a la pagina solicitada, llevar a la main page // Si no es posible redireccionar a la pagina solicitada, llevar a la main page
//response.sendRedirect(request.getScheme() + "://" + RequestDispatcher rd = request.getRequestDispatcher("menu.html");
// request.getServerName() + ":" + request.getServerPort()); rd.include(request,response);
System.out.println("redirigir al index.html");
} else { } else {
//si no es un user valido - mandar error y redireccionar al inicio de sesion //si no es un user valido - mandar error y redireccionar al inicio de sesion
RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
//out.print("<div br align = \"center\" class=\"messageError\" > Credenciales incorrectas! Reintente ... </div>");
rd.include(request,response);
out.println("<p> You may want to <a href='/login.jsp'> try again </a> </p>");
// request.getRequestDispatcher("login.jsp").include(request, response);
// response.sendRedirect("login.jsp");
} }
} }
......
package com.roshka.proyectofinal.login;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.RequestDispatcher;
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 jakarta.servlet.http.HttpSession;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LogoutServlet() {
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());
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//out.print("Has cerrado tu sesion !");
request.getRequestDispatcher("index.html").include(request,response);
HttpSession session = request.getSession(true);
session.invalidate();
out.close();
}
}
\ No newline at end of file
package com.roshka.proyectofinal; package com.roshka.proyectofinal.login;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
import jakarta.servlet.*; import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*; import jakarta.servlet.http.*;
@WebServlet("/protected")
public class ProtectedResource extends HttpServlet { public class ProtectedResource extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
...@@ -22,7 +25,7 @@ public class ProtectedResource extends HttpServlet { ...@@ -22,7 +25,7 @@ public class ProtectedResource extends HttpServlet {
session.setAttribute("login.target", session.setAttribute("login.target",
HttpUtils.getRequestURL(req).toString()); HttpUtils.getRequestURL(req).toString());
res.sendRedirect(req.getScheme() + "://" + req.getServerName() + ":" res.sendRedirect(req.getScheme() + "://" + req.getServerName() + ":"
+ req.getServerPort() + "/login.jsp"); + req.getServerPort() + "/finalProyect2/login.jsp");
return; return;
} }
// El usuario se loggeo y puede ver el recurso // El usuario se loggeo y puede ver el recurso
......
...@@ -26,6 +26,8 @@ ...@@ -26,6 +26,8 @@
<li class="link-menu"><a href="">Home</a></li> <li class="link-menu"><a href="">Home</a></li>
<li class="link-menu"><a href="formulario.jsp">Postulate</a></li> <li class="link-menu"><a href="formulario.jsp">Postulate</a></li>
<li class="link-menu"><a href="formulario_bootcamp.jsp">Crear bootcamp</a> <li class="link-menu"><a href="formulario_bootcamp.jsp">Crear bootcamp</a>
<li class="link-menu"><a href="login.jsp">Login</a>
<li class="link-menu"><a href="protected">Recurso Protegido</a></li>
</li> </li>
</ul> </ul>
</div> </div>
......
...@@ -14,6 +14,10 @@ ...@@ -14,6 +14,10 @@
</h1> </h1>
<br/> <br/>
<a href="hello-servlet">Hello Servlet</a><br> <a href="hello-servlet">Hello Servlet</a><br>
<a href="login.jsp">LOGIN</a><br>
<a href="logout">LOGOUT</a><br>
<a href="protected">RECUSO PROTEGIDO</a><br>
</body> </body>
......
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!--
<!DOCTYPE html> Follow me on
<html> ------------
<head> Codepen: https://codepen.io/mycnlz/
<title>BootcampsLogin</title> Dribbble: https://dribbble.com/mycnlz
</head> Pinterest: https://pinterest.com/mycnlz/
<body> -->
<div align=" center">
<h1>User Login Form</h1> <div class='box'>
<div class='box-form'>
<div class='box-login-tab'></div>
<div class='box-login-title'>
<div class='i i-login'></div><h2> USUARIO </h2>
<link rel="stylesheet" href="usrebe.css">
<link rel="stylesheet" href="usrebe.js">
</div>
<form action="login" method="post"> <form action="login" method="post">
<table align = "center"> <div class='box-login'>
<tr><td>Correo:</td> <td><input type="text" name = "correo"></td></tr> <div class='fieldset-body' id='login_form'>
<tr><td>Password:</td><td><input type="password" name="password"></td></tr> <button onclick="openLoginInfo();" class='b b-form i i-more' title='Mais Informações'></button>
<p class='field'>
<label for='user'>E-MAIL</label>
<input type='text' id='correo' name='correo' title='Correo' />
<span id='valida' class='i i-warning'></span>
</p>
<p class='field'>
<label for='password'>PASSWORD</label>
<input type='password' id='password' name='password' title='Password' />
<span id='valida' class='i i-close'></span>
</p>
<tr><td><input type="submit" value="Login"/></td></tr> <input type='submit' id='do_login' value='INICIAR SESION' title='INICIAR SESION' />
</table> </div>
</div>
</div>
</form> </form>
<div class='box-info'>
<p><button onclick="closeLoginInfo();" class='b b-info i i-left' title='Back to Sign In'></button><h3>Need Help?</h3>
</p>
<div class='line-wh'></div>
<button onclick="" class='b-support' title='Forgot Password?'> Forgot Password?</button>
<button onclick="" class='b-support' title='Contact Support'> Contact Support</button>
<div class='line-wh'></div>
<button onclick="" class='b-cta' title='Sign up now!'> CREATE ACCOUNT</button>
</div> </div>
</div>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment