Estoy empezando a conocer el desarrollo java web y estoy desarrollando un ejercicio el cual debo llenar un array
desde un formulario jsp
, enviarlo a un servlet
para llenar con los datos del formulario el array
y mostrar los resultados en una tabla de otro jsp
,
Ejemplo:
En index.jsp
tengo el form
, lo envío a ListarPrendas.java
y lo muestro en ListadoDePrendas.jsp
, sin embargo intenté usar RequestDispatcher
forward
para procesar el array
en el servlet
y solo pedir datos en el listado.jsp
, pero no pasa nada, aquí les dejo el código de las 3 paginas:
index.jsp
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<a href="index.jsp">Inicio</a> <a href="ListadoDePrendas.jsp">Listar Prendas</a>
<form action="ListarPrendas" method="POST">
<table>
<tr>
<td>Codigo:</td><td><input type="text" name="codigo" required="Falta campo!"></td>
</tr>
<tr>
<td>Nombre:</td><td><input type="text" name="nombre" required="Falta campo!"></td>
</tr>
<tr>
<td>Marca:</td><td><input type="text" name="marca" required="Falta campo!"></td>
</tr>
<tr>
<td>Talla:</td>
<td>
<select name="talla">
<option value="S">Small</option>
<option value="M">Medium</option>
<option value="L">Large</option>
</select>
</td>
</tr>
<tr>
<td>Stock:</td><td><input type="text" name="stock" required="Falta campo!"></td>
</tr>
<tr>
<td>Temporada:</td>
<td>
<select name="temporada">
<option value="true">SI</option>
<option value="false">NO</option>
</select>
</td>
</tr>
<tr>
<td><button type="submit">Agregar</button></td><td><button type="reset">Limpiar</button></td>
</tr>
</table>
</form>
</body>
</html>
servlet ListarPrendas.java
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
response.setContentType("text/html;charset=UTF-8");
//
Prenda ropa = new Prenda();
ropa.setCodigo(Integer.valueOf(request.getParameter("Codigo")));
ropa.setNombre(request.getParameter("Nombre"));
ropa.setMarca(request.getParameter("Marca"));
char t = request.getParameter("Talla").charAt(0);
ropa.setTalla(t);
//hora
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
String fecha = dateFormat.format(date);
ropa.setFechaIngreso(fecha);
ropa.setStock(Integer.valueOf(request.getParameter("Stock")));
ropa.setTemporada(Boolean.valueOf(request.getParameter("Temporada")));
//agregar a la lista
Biblioteca.prenda.add(ropa);
//
for(Prenda cloth:Biblioteca.prenda){
request.setAttribute("codigo", cloth.getCodigo());
request.setAttribute("nombre", cloth.getNombre());
request.setAttribute("marca", cloth.getMarca());
request.setAttribute("talla", cloth.getTalla());
request.setAttribute("fecha", cloth.getFechaIngreso());
request.setAttribute("stock", cloth.getStock());
request.setAttribute("temporada", cloth.isTemporada());
}
RequestDispatcher despachador = request.getRequestDispatcher("ListadoDePrendas.jsp");
despachador.forward(request, response);
}
ListadoDePrendas.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Mis Prendas</h1>
<table border="1">
<thead>
<th>Codigo</th>
<th>Nombre</th>
<th>Marca</th>
<th>Talla</th>
<th>Fecha Ingreso</th>
<th>Stock</th>
<th>Temporada</th>
</thead>
<tbody>
<tr>
<td><%= request.getAttribute("codigo") %></td>
<td><%= request.getAttribute("nombre") %></td>
<td><%= request.getAttribute("marca") %></td>
<td><%= request.getAttribute("talla") %></td>
<td><%= request.getAttribute("fecha") %></td>
<td><%= request.getAttribute("stock") %></td>
<td><%= request.getAttribute("temporada") %></td>
<td></td>
</tr>
</tbody>
</table>
<a href="index.jsp">Volver</a>
</body>
</html>