Java Web desde cero en Netbeans ☁️[19.- JSP Centralizar codigo en packages]
Es este tutorial vamso a centralizar nuestro codigo en los paquetes (packages) para evitar repetir nuestro codigo en todos lados.
Codigo: https://github.com/programadornovato/javaWeb/commit/1d5e57fea127c55c9c731f18664750bf0a1f3df4
package Utils; import java.math.BigInteger; import java.security.MessageDigest; public class Encriptar { public String getMD5(String input) { try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] encBytes = md.digest(input.getBytes()); BigInteger numero = new BigInteger(1, encBytes); String encString = numero.toString(16); while (encString.length() < 32) { encString = "0" + encString; } return encString; } catch (Exception e) { throw new RuntimeException(e); } } } <%@page import="java.sql.*"%> <%@page import="Utils.Encriptar" %> <%@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="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous"> <title>Usuario</title> </head> <body> <% HttpSession sesion = request.getSession(); if (sesion.getAttribute("logueado") == null || sesion.getAttribute("logueado").equals("0")) { response.sendRedirect("login.jsp"); } Connection con = null; Statement st = null; Encriptar enc=new Encriptar(); %> <div class="container mt-5"> <div class="row"> <div class="col-sm"> <form action="datosUsuario.jsp" method="post"> <div class="form-group"> <label >User</label> <input type="text" class="form-control" name="user" placeholder="user" value="<%= sesion.getAttribute("user")%>" > </div> <div class="form-group"> <label >Password</label> <input type="text" class="form-control" name="password1" placeholder="password"> </div> <div class="form-group"> <label >Respita su password</label> <input type="text" class="form-control" name="password2" placeholder="Repita su password"> </div> <button type="submit" name="guardar" class="btn btn-primary">Guardar</button> <a href="index.jsp" class="btn btn-danger">Cancelar</a> </form> </div> </div> </div> </body> <% if (request.getParameter("guardar") != null) { String user = request.getParameter("user"); String password1 = request.getParameter("password1"); String password2 = request.getParameter("password2"); if (password1.equals(password2)) { try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/jsp?user=eugenio&password=123456"); st = con.createStatement(); st.executeUpdate("update user set user='" + user + "',password='" + enc.getMD5(password1) + "' where id='" + sesion.getAttribute("id") + "';"); sesion.setAttribute("user", user); response.sendRedirect("index.jsp"); } catch (Exception e) { out.print(e); } } else{ out.print("Password no coincide"); } } %> </html> <%@page import="java.sql.*"%> <%@page import="Utils.Encriptar" %> <%@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="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous"> <title>Login</title> </head> <body> <div class="container mt-5"> <div class="row"> <div class="col-sm"> <form method="post" action="login.jsp" > <div class="form-group"> <label>Usuario</label> <input type="text" class="form-control" name="user" placeholder="Humano pon tu usuario"> </div> <div class="form-group"> <label>Password</label> <input type="password" class="form-control" name="password" placeholder="Humano pon tu password"> </div> <button type="submit" class="btn btn-primary" name="login" >Login</button> </form> <% Connection con = null; Statement st = null; ResultSet rs = null; Encriptar enc=new Encriptar(); if (request.getParameter("login") != null) { String user = request.getParameter("user"); String password = request.getParameter("password"); HttpSession sesion = request.getSession(); try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/jsp?user=eugenio&password=123456"); st = con.createStatement(); rs = st.executeQuery(" SELECT * FROM `user` where user='" + user + "' and password='" + enc.getMD5(password) + "'; "); while (rs.next()) { sesion.setAttribute("logueado", "1"); sesion.setAttribute("user", rs.getString("user")); sesion.setAttribute("id", rs.getString("id")); response.sendRedirect("index.jsp"); } out.print(" <div class=\"alert alert-danger\" role=\"alert\"> Usuario no valido </div>"); } catch (Exception e) { } } %> </div> </div> </div> </body> </html>
Curso de Java de 0 a 100: https://www.youtube.com/playlist?list=PLCTD_CpMeEKTT-qEHGqZH3fkBgXH4GOTF
? Esta lista de reproducción: https://www.youtube.com/playlist?list=PLCTD_CpMeEKRAgcBmPee0Wjx5HsJ0nb0L
Codigos en gdrive: https://drive.google.com/file/d/10uLG9o2oDV-qB32G4kMIpzXgLCiUYaYz/view?usp=sharing
Gracias por apoyar este canal: https://www.patreon.com/programadornovato?fan_landing=true
? Facebook: https://facebook.com/ProgramadorNovatoOficial
? Twitter: https://twitter.com/programadornova
? Linkedin: https://www.linkedin.com/in/programadornovato/
? Instagram: https://www.instagram.com/programadornovato/