Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anteriorRevisión previaPróxima revisión | Revisión previa | ||
| dad2:ejercicio_login [2024/02/20 10:48] – thejuanvisu | dad2:ejercicio_login [2024/02/20 11:49] (actual) – thejuanvisu | ||
|---|---|---|---|
| Línea 4: | Línea 4: | ||
| {{: | {{: | ||
| </ | </ | ||
| - | ===== Implementación ===== | + | ===== Implementación |
| + | Implementamos el Front End de la aplicación en archivos .jsp: | ||
| + | < | ||
| + | {{: | ||
| + | </ | ||
| ==== index.jsp ==== | ==== index.jsp ==== | ||
| <code html index.jsp> | <code html index.jsp> | ||
| Línea 23: | Línea 27: | ||
| <input type =" | <input type =" | ||
| <a href=" | <a href=" | ||
| + | <% | ||
| + | String msg = (String) request.getAttribute(" | ||
| + | if (msg != null){ | ||
| + | out.println(msg); | ||
| + | } | ||
| + | |||
| + | %> | ||
| + | |||
| + | |||
| </ | </ | ||
| </ | </ | ||
| </ | </ | ||
| + | |||
| + | ==== insertar.jsp ==== | ||
| + | |||
| + | <code html insertar.jsp> | ||
| + | <%@ page language=" | ||
| + | pageEncoding=" | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <meta charset=" | ||
| + | < | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | <form action=" | ||
| + | Nombre:< | ||
| + | Clave:< | ||
| + | <input type =" | ||
| + | <a href=" | ||
| + | |||
| + | <% | ||
| + | Hashtable< | ||
| + | |||
| + | if (usuarios != null){ | ||
| + | for(Usuario usuario : usuarios.values()){ | ||
| + | out.println("< | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | %> | ||
| + | |||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ===== Implementación Java ===== | ||
| + | |||
| + | ==== Usuario.java ==== | ||
| + | <code java usuario.java> | ||
| + | package funciones; | ||
| + | |||
| + | public class Usuario { | ||
| + | private String username; | ||
| + | private String pass; | ||
| + | public Usuario(String username, String pass) { | ||
| + | super(); | ||
| + | this.username = username; | ||
| + | this.pass = pass; | ||
| + | } | ||
| + | public String getUsername() { | ||
| + | return username; | ||
| + | } | ||
| + | public void setUsername(String username) { | ||
| + | this.username = username; | ||
| + | } | ||
| + | public String getPass() { | ||
| + | return pass; | ||
| + | } | ||
| + | public void setPass(String pass) { | ||
| + | this.pass = pass; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Implementación Servlets ===== | ||
| + | ==== login.java ==== | ||
| + | <code java login.java> | ||
| + | package funciones; | ||
| + | |||
| + | 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 java.io.IOException; | ||
| + | import java.util.Hashtable; | ||
| + | |||
| + | |||
| + | |||
| + | /** | ||
| + | * Servlet implementation class Login | ||
| + | */ | ||
| + | public class Login extends HttpServlet { | ||
| + | private static final long serialVersionUID = 1L; | ||
| + | |||
| + | /** | ||
| + | * @see HttpServlet# | ||
| + | */ | ||
| + | public Login() { | ||
| + | super(); | ||
| + | // TODO Auto-generated constructor stub | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * @see HttpServlet# | ||
| + | */ | ||
| + | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, | ||
| + | Hashtable< | ||
| + | if(usuarios == null || usuarios.get(request.getParameter(" | ||
| + | || !(usuarios.get(request.getParameter(" | ||
| + | request.setAttribute(" | ||
| + | request.getRequestDispatcher(" | ||
| + | }else { | ||
| + | |||
| + | request.getRequestDispatcher(" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * @see HttpServlet# | ||
| + | */ | ||
| + | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, | ||
| + | // TODO Auto-generated method stub | ||
| + | doGet(request, | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ==== InsertarUsuario.java ==== | ||
| + | <code java InsertarUsuario.java> | ||
| + | package funciones; | ||
| + | |||
| + | 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 java.io.IOException; | ||
| + | import java.util.Hashtable; | ||
| + | |||
| + | import jakarta.servlet.ServletConfig; | ||
| + | |||
| + | /** | ||
| + | * Servlet implementation class InsertarUsuario | ||
| + | */ | ||
| + | public class InsertarUsuario extends HttpServlet { | ||
| + | private static final long serialVersionUID = 1L; | ||
| + | |||
| + | /** | ||
| + | * @see HttpServlet# | ||
| + | */ | ||
| + | public InsertarUsuario() { | ||
| + | super(); | ||
| + | // TODO Auto-generated constructor stub | ||
| + | } | ||
| + | |||
| + | | ||
| + | @Override | ||
| + | public void init(ServletConfig config) throws ServletException { | ||
| + | super.init(config); | ||
| + | Hashtable< | ||
| + | usuarios.put(" | ||
| + | | ||
| + | config.getServletContext().setAttribute(" | ||
| + | |||
| + | } | ||
| + | |||
| + | | ||
| + | | ||
| + | /** | ||
| + | * @see HttpServlet# | ||
| + | */ | ||
| + | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, | ||
| + | Hashtable< | ||
| + | |||
| + | if(usuarios == null) { | ||
| + | usuarios = new Hashtable< | ||
| + | request.getServletContext().setAttribute(" | ||
| + | } | ||
| + | |||
| + | Usuario usuario = new Usuario(request.getParameter(" | ||
| + | usuarios.put(usuario.getUsername(), | ||
| + | |||
| + | |||
| + | System.out.println(" | ||
| + | |||
| + | request.getRequestDispatcher(" | ||
| + | } | ||
| + | |||
| + | |||
| + | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, | ||
| + | // TODO Auto-generated method stub | ||
| + | doGet(request, | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||