Muestra las diferencias entre dos versiones de la página.
| Próxima revisión | Revisión previa | ||
| dad2:servlets [2024/02/01 12:43] – creado thejuanvisu | dad2:servlets [2024/02/06 10:20] (actual) – thejuanvisu | ||
|---|---|---|---|
| Línea 1: | Línea 1: | ||
| ====== Servlets ====== | ====== Servlets ====== | ||
| - | Son procesos sin interfaz gráfica ejecutados en servidor a petición de un cliente, en nuestro caso, peticiones http. | + | |
| + | |||
| + | |||
| + | Son procesos sin interfaz gráfica ejecutados en servidor a petición de un cliente, en nuestro caso, peticiones http. | ||
| Para crear un servlet debemos ir en la carpeta de nuestro proyecto a "Java Resources/ | Para crear un servlet debemos ir en la carpeta de nuestro proyecto a "Java Resources/ | ||
| Línea 24: | Línea 27: | ||
| </ | </ | ||
| + | Para comprobar el funcionamiento correcto del servlet añadiremos las siguientes 2 líneas: | ||
| + | < | ||
| + | {{: | ||
| + | </ | ||
| + | <code java holamundo.java> | ||
| + | package com.organizacion.servlets; | ||
| + | |||
| + | 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; | ||
| + | |||
| + | /** | ||
| + | * Servlet implementation class Hola_Mundo | ||
| + | */ | ||
| + | public class Hola_Mundo extends HttpServlet { | ||
| + | private static final long serialVersionUID = 1L; | ||
| + | |||
| + | /** | ||
| + | * @see HttpServlet# | ||
| + | */ | ||
| + | public Hola_Mundo() { | ||
| + | super(); | ||
| + | // TODO Auto-generated constructor stub | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * @see HttpServlet# | ||
| + | */ | ||
| + | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, | ||
| + | // TODO Auto-generated method stub | ||
| + | System.out.println(" | ||
| + | response.getWriter().append(" | ||
| + | // | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * @see HttpServlet# | ||
| + | */ | ||
| + | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, | ||
| + | // TODO Auto-generated method stub | ||
| + | doGet(request, | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | Tras eso apagaremos el servidor de Tomcat y lo iniciaremos de nuevo al darle a arrancar proyecto, tras eso se nos debería abrir el navegador con lo siguiente: | ||
| + | < | ||
| + | {{: | ||
| + | </ | ||
| + | Y en la consola del servidor debería aparecer esto otro: | ||
| + | < | ||
| + | {{: | ||
| + | </ | ||