Base de Datos
create database login; use login; create table usuario( idUsuario int primary key, nombreUsuario varchar(50), clave varchar(50), tipoUsuario varchar(50)/* Administrador || Cliente*/ ); insert into usuario values(1,'admin','123','Administrador'); insert into usuario values(2,'cliente','321','Cliente');Clase Conexion
package com.conexion; import java.sql.*; /** * Nombre de Clase: Conexion. * Fecha: 29-09-2017. * Version: 1.0. * Copyright: ITCA-FEPADE. * @author Victor Alvarado */ public class Conexion { private Connection con; public Connection getCon() { return con; } public void setCon(Connection con) { this.con = con; } public void conectar() throws Exception { try { Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost:3306/login?user=root&password="); } catch (ClassNotFoundException | SQLException e) { throw e; } } public void desconectar() throws Exception { try { if(con!=null) { if(con.isClosed()==false) { con.isClosed(); } } } catch (SQLException e) { throw e; } } }
Clase Usuario
package com.modelo; /** * Nombre de Clase: Usuario. * Fecha: 29-09-2017. * Version: 1.0. * Copyright: ITCA-FEPADE. * @author Victor Alvarado */ public class Usuario { private int idUsuario; private String nombreUsuario; private String clave; private String tipoUsuario; public Usuario() { } public Usuario(int idUsuario, String nombreUsuario, String clave, String tipoUsuario) { this.idUsuario = idUsuario; this.nombreUsuario = nombreUsuario; this.clave = clave; this.tipoUsuario = tipoUsuario; } public int getIdUsuario() { return idUsuario; } public void setIdUsuario(int idUsuario) { this.idUsuario = idUsuario; } public String getNombreUsuario() { return nombreUsuario; } public void setNombreUsuario(String nombreUsuario) { this.nombreUsuario = nombreUsuario; } public String getClave() { return clave; } public void setClave(String clave) { this.clave = clave; } public String getTipoUsuario() { return tipoUsuario; } public void setTipoUsuario(String tipoUsuario) { this.tipoUsuario = tipoUsuario; } }
Clase AccederUsuario
package com.modelo; import com.conexion.Conexion; import java.sql.PreparedStatement; import java.sql.ResultSet; /** * Nombre de Clase: AccederUsuario. * Fecha: 29-09-2017. * Version: 1.0. * Copyright: ITCA-FEPADE. * @author Victor Alvarado */ public class AccederUsuario extends Conexion { public String login(Usuario usu)throws Exception { String estado = ""; ResultSet rs; try { this.conectar(); String sql = "select tipoUsuario from usuario where nombreUsuario=? and clave=?"; PreparedStatement ps =this.getCon().prepareStatement(sql); ps.setString(1, usu.getNombreUsuario()); ps.setString(2, usu.getClave()); rs= ps.executeQuery(); if (rs.next()) { estado = "true"; } usu.setTipoUsuario(rs.getString("tipoUsuario")); } catch (Exception e) { throw e; } return estado; } }
Pagina index
<%-- Document : index Created on : 09-29-2017, 04:49:38 PM Author : VA --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="font/css/font-awesome.min.css"> <link rel='stylesheet prefetch' href='bootstrap/css/bootstrap.min.css'> <link rel="stylesheet" href="css/style.css"> <title>.::Login::.</title> </head> <body> <div class="wrapper"> <form class="form-signin" action="accesoLogin" method="POST"> <center><span class="fa fa-user fa-5x"></span></center> <input type="text" class="form-control" name="nombreUsuario" placeholder="Nombre de Usuario" required="" autofocus="" /> <input type="password" class="form-control" name="clave" placeholder="Clave" required=""/> <button class="btn btn-lg btn-primary btn-block" type="submit" name="btnAcceder">Acceder</button> </form> </div> </body> </html>
Pagina vistaAdmin
<%-- Document : vistaAdmin Created on : 09-29-2017, 04:53:37 PM Author : VA --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@page session="true" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="../font/css/font-awesome.min.css"> <link rel='stylesheet prefetch' href='../bootstrap/css/bootstrap.min.css'> <title>Admin</title> </head> <% String user = ""; HttpSession objSesion = request.getSession(); String usuario; if (objSesion.getAttribute("usuario") != null && objSesion.getAttribute("nivel") == "Administrador") { usuario = objSesion.getAttribute("usuario").toString(); user = "<label>" + usuario + "</label>"; } else if (objSesion.getAttribute("usuario") != null && objSesion.getAttribute("nivel") == "Cliente") { out.print("<script>location.replace('vistaCliente.jsp');</script>"); } else { out.print("<script>location.replace('../index.jsp');</script>"); } %> <body> <center> <h1>Vista Administrador</h1> <h1>Bienvenido <% out.print(user);%></h1> <h3>El administrador tiene acceso total a todo </h3> <a href="vistaCliente.jsp" class="btn btn-primary"><span class="fa fa-eye"></span> Vista Cliente</a> <a href="cerrarSesion.jsp" class="btn btn-danger"><span class="fa fa-sign-out"></span> Cerrar sesion</a> </center> </body> </html>
Pagina vistaCiente
<%-- Document : vistaCliente Created on : 09-29-2017, 04:53:50 PM Author : VA --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@page session="true" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="../font/css/font-awesome.min.css"> <link rel='stylesheet prefetch' href='../bootstrap/css/bootstrap.min.css'> <title>Cliente</title> </head> <% String user = ""; String admin = ""; HttpSession objSesion = request.getSession(); String usuario; if (objSesion.getAttribute("usuario") != null && objSesion.getAttribute("nivel") == "Cliente") { usuario = objSesion.getAttribute("usuario").toString(); user = "<label>" + usuario + "</label>"; } else if (objSesion.getAttribute("usuario") != null && objSesion.getAttribute("nivel") == "Administrador") { usuario = objSesion.getAttribute("usuario").toString(); user = "<label>" + usuario + "</label>"; admin = "<label>El administrador tiene acceso total a todo <br><a href='vistaAdmin.jsp' class='btn btn-primary'><span class='fa fa-eye'></span> Vista administrador</a></label>"; } else { out.print("<script>location.replace('../index.jsp');</script>"); } %> <body> <center><h1>Vista Cliente</h1> <% out.print(admin); %> <h1>Bienvenido <% out.print(user);%></h1> <a href="cerrarSesion.jsp" class="btn btn-danger"><span class="fa fa-sign-out"></span> Cerrar sesion</a> </center> </body> </html>
Pagina error
<%-- Document : error Created on : 09-29-2017, 05:34:57 PM Author : VA --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="../font/css/font-awesome.min.css"> <link rel='stylesheet prefetch' href='../bootstrap/css/bootstrap.min.css'> <title>Error</title> </head> <body> <br> <br> <center> <h1 style="color: #0275d8;"><span style="color: red" class="fa fa-user-times"></span><br> Su usuario o clave son incorrectos</h1> <a href="../index.jsp" class="btn btn-primary"><span class="fa fa-user-circle"></span> Login</a> </center> </body> </html>
Pagina cerrarSesion
<%-- Document : cerrarSesion Created on : 09-29-2017, 04:53:15 PM Author : VA --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@page session="true" %> <% HttpSession objSesion = request.getSession(); objSesion.invalidate(); out.print("<script>location.replace('../index.jsp');</script>"); %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> </body> </html>
Clase-Servlet AccesoLogin
package com.controlador; import com.modelo.AccederUsuario; import com.modelo.Usuario; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Nombre de Clase: AccesoLogin. Fecha: 29-09-2017. Version: 1.0. Copyright: * ITCA-FEPADE. * * @author Victor Alvarado */ public class AccesoLogin extends HttpServlet { /** * 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"); PrintWriter out = response.getWriter(); if (request.getParameter("btnAcceder") != null) { Usuario usu = new Usuario(); String user = request.getParameter("nombreUsuario"); String clave = request.getParameter("clave"); usu.setNombreUsuario(user); usu.setClave(clave); AccederUsuario login = new AccederUsuario(); String estado; try { estado = login.login(usu); if ("true".equals(estado)) { HttpSession objSesion = request.getSession(); if (usu.getTipoUsuario().equals("Administrador")) { objSesion.setAttribute("usuario", user); objSesion.setAttribute("nivel", "Administrador"); response.sendRedirect("jsp/vistaAdmin.jsp"); } else if (usu.getTipoUsuario().equals("Cliente")) { objSesion.setAttribute("usuario", user); objSesion.setAttribute("nivel", "Cliente"); response.sendRedirect("jsp/vistaCliente.jsp"); } } } catch (Exception ex) { response.sendRedirect("./jsp/error.jsp"); } } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }
Vista Login
Vista Error
Vista Administrador
El administrador tiene acceso total
Vista Cliente
Descargar Archivo


Follow Us
Were this world an endless plain, and by sailing eastward we could for ever reach new distances