Muestra las diferencias entre dos versiones de la página.
| Próxima revisión | Revisión previa | ||
| dad2:servlets2 [2024/02/06 10:33] – creado thejuanvisu | dad2:servlets2 [2024/02/06 11:32] (actual) – thejuanvisu | ||
|---|---|---|---|
| Línea 27: | Línea 27: | ||
| <?xml version=" | <?xml version=" | ||
| <web-app xmlns: | <web-app xmlns: | ||
| - | < | + | <servlet><!-- Declaramos un nuevo servlet --> |
| < | < | ||
| < | < | ||
| < | < | ||
| < | < | ||
| - | </ | + | </ |
| - | < | + | < |
| - | < | + | < |
| - | < | + | < |
| </ | </ | ||
| - | < | + | < |
| - | < | + | < |
| < | < | ||
| < | < | ||
| Línea 46: | Línea 46: | ||
| < | < | ||
| </ | </ | ||
| - | < | + | |
| + | |||
| + | | ||
| < | < | ||
| < | < | ||
| < | < | ||
| < | < | ||
| - | </ | + | </ |
| < | < | ||
| < | < | ||
| Línea 59: | Línea 61: | ||
| </ | </ | ||
| </ | </ | ||
| + | |||
| + | ===== Obtencion de parámetros mediante Query String ===== | ||
| + | |||
| + | Para obtener parámetros después de la url añadimos un "?" | ||
| + | < | ||
| + | localhost: | ||
| + | </ | ||
| + | |||
| + | Para hacer funcionar esto debemos añadir lo siguiente a nuestro código dentro de doGet: | ||
| + | <code java> | ||
| + | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, | ||
| + | // TODO Auto-generated method stub | ||
| + | |||
| + | String paramName =""; | ||
| + | response.getWriter().append("< | ||
| + | |||
| + | for(Enumeration< | ||
| + | paramName=params.nextElement(); | ||
| + | response.getWriter().append(paramName + ":" | ||
| + | } | ||
| + | |||
| + | response.getWriter().append("</ | ||
| + | |||
| + | |||
| + | // | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | El código completo sería el siguiente: | ||
| + | <code java> | ||
| + | package com.organizacion.servlets; | ||
| + | |||
| + | import jakarta.servlet.ServletContext; | ||
| + | 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 jakarta.websocket.Extension.Parameter; | ||
| + | |||
| + | import java.io.IOException; | ||
| + | import java.util.Enumeration; | ||
| + | |||
| + | /** | ||
| + | * Servlet implementation class MiSegundoSevlet | ||
| + | */ | ||
| + | public class MiSegundoSevlet extends HttpServlet { | ||
| + | private static final long serialVersionUID = 1L; | ||
| + | |||
| + | /** | ||
| + | * @see HttpServlet# | ||
| + | */ | ||
| + | public MiSegundoSevlet() { | ||
| + | super(); | ||
| + | // TODO Auto-generated constructor stub | ||
| + | } | ||
| + | |||
| + | /** | ||
| + | * @see HttpServlet# | ||
| + | */ | ||
| + | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, | ||
| + | // TODO Auto-generated method stub | ||
| + | |||
| + | String paramName =""; | ||
| + | response.getWriter().append("< | ||
| + | |||
| + | for(Enumeration< | ||
| + | paramName=params.nextElement(); | ||
| + | response.getWriter().append(paramName + ":" | ||
| + | } | ||
| + | |||
| + | response.getWriter().append("</ | ||
| + | |||
| + | |||
| + | // | ||
| + | |||
| + | } | ||
| + | |||
| + | /** | ||
| + | * @see HttpServlet# | ||
| + | */ | ||
| + | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, | ||
| + | // TODO Auto-generated method stub | ||
| + | doGet(request, | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | Este código nos mostrará lo siguiente en el navegador: | ||
| + | < | ||
| + | {{: | ||
| + | </ | ||