Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anteriorRevisión previaPróxima revisión | Revisión previa | ||
| dad2:jsp [2024/02/13 11:05] – thejuanvisu | dad2:jsp [2024/02/21 12:21] (actual) – thejuanvisu | ||
|---|---|---|---|
| Línea 32: | Línea 32: | ||
| </ | </ | ||
| </ | </ | ||
| + | |||
| + | ===== Etiquetas de JSP ===== | ||
| + | |||
| + | <code html> | ||
| + | <%@ Directiva de configuración de la página%> | ||
| + | <%@ page errorPage = " | ||
| + | |||
| + | <%! Declaración de variables y funciones%> | ||
| + | <%! int i=0; %> | ||
| + | | ||
| + | <%Bloque de código Java%> | ||
| + | < | ||
| + | | ||
| + | | ||
| + | </ | ||
| + | |||
| + | ===== Modificaciones del web.xml para declaración de servlet ===== | ||
| + | <code xml web.xml> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ===== Modelo Vista Controlador (MVC) y Paso de Atributos ===== | ||
| + | Esta parte se centra en la capa de negocio. | ||
| + | ==== Control ==== | ||
| + | Es un servlet único que recibirá todas las peticiones y las redirigirá a otra clase. Este servlet comprueba los permisos e invoca las funciones o modelos correspondientes. Para interactuar con el control se usa la URL seguida de un "?" | ||
| + | < | ||
| + | http:// | ||
| + | </ | ||
| + | En esta URL sería Control el Servlet que controla todas las peticiones y LEER_CORREO el identificador de modelo o acción a ejecutar. | ||
| + | |||
| + | ==== Modelo ==== | ||
| + | Realiza toda la funcionalidad, | ||
| + | {{drawio> | ||
| + | ===== Implementación MVC ===== | ||
| + | El proyecto seguirá la siguiente estructura en la que separaremos por un lado el servlet control y por otro lado las clases: | ||
| + | < | ||
| + | {{: | ||
| + | </ | ||
| + | ==== Servlet Control ==== | ||
| + | |||
| + | <code java Control.java> | ||
| + | package clases; | ||
| + | |||
| + | import jakarta.servlet.ServletConfig; | ||
| + | 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 acciones.Accion; | ||
| + | import acciones.InsertarUsuario; | ||
| + | |||
| + | /** | ||
| + | * Servlet implementation class Control | ||
| + | */ | ||
| + | public class Control extends HttpServlet { | ||
| + | private static final long serialVersionUID = 1L; | ||
| + | private Hashtable< | ||
| + | |||
| + | public static final String PARAM_ACTION_ID = " | ||
| + | |||
| + | /** | ||
| + | * @see HttpServlet# | ||
| + | */ | ||
| + | public Control() { | ||
| + | super(); | ||
| + | // TODO Auto-generated constructor stub | ||
| + | } | ||
| + | | ||
| + | |||
| + | @Override | ||
| + | public void init(ServletConfig config) throws ServletException { | ||
| + | // TODO Auto-generated method stub | ||
| + | |||
| + | super.init(config); | ||
| + | this.acciones = new Hashtable< | ||
| + | this.acciones.put(" | ||
| + | } | ||
| + | |||
| + | |||
| + | /** | ||
| + | * @see HttpServlet# | ||
| + | */ | ||
| + | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, | ||
| + | String jsp = " | ||
| + | String paramIdAccion = request.getParameter(Control.PARAM_ACTION_ID);// | ||
| + | Accion accion = this.acciones.get(paramIdAccion);// | ||
| + | jsp = accion.ejecutar(request, | ||
| + | request.getRequestDispatcher(jsp).forward(request, | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * @see HttpServlet# | ||
| + | */ | ||
| + | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, | ||
| + | // TODO Auto-generated method stub | ||
| + | doGet(request, | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ==== Clase Accion ==== | ||
| + | |||
| + | <code java Accion.java> | ||
| + | package acciones; | ||
| + | |||
| + | import jakarta.servlet.http.HttpServlet; | ||
| + | import jakarta.servlet.http.HttpServletRequest; | ||
| + | import jakarta.servlet.http.HttpServletResponse; | ||
| + | |||
| + | public abstract class Accion { | ||
| + | //Esta es la clase Padre | ||
| + | public abstract String ejecutar(HttpServletRequest request, HttpServletResponse response) throws ServletException, | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ==== Clase InsertarUsuario ==== | ||
| + | <code java InsertarUsuario.java> | ||
| + | package acciones; | ||
| + | import java.io.IOException; | ||
| + | |||
| + | import jakarta.servlet.ServletException; | ||
| + | import jakarta.servlet.http.HttpServletRequest; | ||
| + | import jakarta.servlet.http.HttpServletResponse; | ||
| + | |||
| + | public class InsertarUsuario extends Accion { | ||
| + | |||
| + | @Override | ||
| + | public String ejecutar(HttpServletRequest request, HttpServletResponse response) | ||
| + | throws ServletException, | ||
| + | |||
| + | System.out.println(" | ||
| + | |||
| + | return " | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | ==== index.jsp ==== | ||
| + | <code html index.jsp> | ||
| + | <%@page import=" | ||
| + | <%@ page language=" | ||
| + | pageEncoding=" | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <meta charset=" | ||
| + | < | ||
| + | </ | ||
| + | < | ||
| + | <a href=" | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ==== destino.jsp ==== | ||
| + | |||
| + | <code html destino.jsp> | ||
| + | <%@ page language=" | ||
| + | pageEncoding=" | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <meta charset=" | ||
| + | < | ||
| + | </ | ||
| + | < | ||
| + | Destinooo | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||