Knoppia

Wiki de Informática y otras historias

Herramientas de usuario

Herramientas del sitio


dad2:ejercicio_login

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anteriorRevisión previa
Próxima revisión
Revisión previa
dad2:ejercicio_login [2024/02/20 10:50] thejuanvisudad2:ejercicio_login [2024/02/20 11:49] (actual) thejuanvisu
Línea 5: Línea 5:
 </WRAP> </WRAP>
 ===== Implementación Web ===== ===== Implementación Web =====
 +Implementamos el Front End de la aplicación en archivos .jsp:
 +<WRAP>
 +{{:dad2:pasted:20240220-105545.png}}
 +</WRAP>
 ==== index.jsp ==== ==== index.jsp ====
 <code html index.jsp> <code html index.jsp>
Línea 23: Línea 27:
   <input type ="submit"/>   <input type ="submit"/>
   <a href="registro.html">Formulario de registro</a>   <a href="registro.html">Formulario de registro</a>
 +<%
 + String msg = (String) request.getAttribute("MSG");
 + if (msg != null){
 + out.println(msg);
 + }
 +
 +%>
 + 
 +
  
 </body> </body>
Línea 31: Línea 44:
  
 <code html insertar.jsp> <code html insertar.jsp>
 +<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 +    pageEncoding="ISO-8859-1"%>
 +<!DOCTYPE html>
 +<html>
 +<head>
 +<meta charset="ISO-8859-1">
 +<title>Insert title here</title>
 +</head>
 +<body>
 + <h1>Registro</h1>
 +  <form action="InsertarUsuario" method="registrar">
 +  Nombre:<input type ="text" name="usuario" /><br>
 +  Clave:<input type = "text" name="pass"/><br>
 +  <input type ="submit"/>
 +  <a href="registro.html">Formulario de registro</a>
 + 
 +<%
 + Hashtable<String, Usuario> usuarios = (Hashtable<String, Usuario>) request.getServletContext().getAttribute("TABLAUSUARIOS");
 +
 + if (usuarios != null){
 + for(Usuario usuario : usuarios.values()){
 + out.println("<br>" + usuario.getUsername());
 + }
 + }
 +
 +
 +%>
 + 
 +</body>
 +</html>
 +</code>
 +
 +===== 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;
 + }
 +}
 +
 +</code>
 +
 +===== 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#HttpServlet()
 +     */
 +    public Login() {
 +        super();
 +        // TODO Auto-generated constructor stub
 +    }
 +
 + /**
 + * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 + */
 + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 + Hashtable<String, Usuario> usuarios = (Hashtable<String, Usuario>) request.getServletContext().getAttribute("TABLAUSUARIOS");
 + if(usuarios == null || usuarios.get(request.getParameter("paramUsername"))== null
 + || !(usuarios.get(request.getParameter("paramUsername"))).getPass().equals(request.getParameter("paramPass"))) {
 + request.setAttribute("MSG", "Login incorrecto");
 + request.getRequestDispatcher("index.jsp").forward(request, response);
 + }else {
 +
 + request.getRequestDispatcher("insertar.jsp").forward(request, response);
 + }
 + }
 +
 + /**
 + * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 + */
 + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 + // TODO Auto-generated method stub
 + doGet(request, response);
 + }
 +
 +}
 +
 +</code>
 +
 +==== 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#HttpServlet()
 +     */
 +    public InsertarUsuario() {
 +        super();
 +        // TODO Auto-generated constructor stub
 +    }
 +
 +    
 + @Override
 + public void init(ServletConfig config) throws ServletException {
 + super.init(config);
 + Hashtable<String, Usuario> usuarios = new Hashtable<String, Usuario>();
 +     usuarios.put("admin", new Usuario("admin","admin"));
 +         
 + config.getServletContext().setAttribute("TABLAUSUARIOS",usuarios);
 +
 + }
 +
 +    
 +    
 + /**
 + * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 + */
 + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 + Hashtable<String, Usuario> usuarios = (Hashtable<String, Usuario>) request.getServletContext().getAttribute("TABLAUSUARIOS");
 +
 + if(usuarios == null) {
 + usuarios = new Hashtable<String, Usuario>();
 + request.getServletContext().setAttribute("TABLAUSUARIOS",usuarios);
 + }
 +
 + Usuario usuario = new Usuario(request.getParameter("paramUsername"), request.getParameter("paramPass"));
 + usuarios.put(usuario.getUsername(), usuario);
 +
 +
 + System.out.println("Usuarios en el sistema: " + usuarios.size());
 +
 + request.getRequestDispatcher("insertar.jsp").forward(request, response);
 + }
 +
 +
 + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 + // TODO Auto-generated method stub
 + doGet(request, response);
 + }
 +
 +}
 +
 </code> </code>
  
dad2/ejercicio_login.1708426221.txt.gz · Última modificación: 2024/02/20 10:50 por thejuanvisu