I am starting to know the java web development and I am developing an exercise which I must fill a array
from a form jsp
, send it to a servlet
to fill with the data of the form the array
and show the results in a table of another jsp
,
Example:
In index.jsp
I have form
, I send it to ListarPrendas.java
and I show it in ListadoDePrendas.jsp
, however I tried to use RequestDispatcher
forward
to process array
in servlet
and only ask for data in listado.jsp
, but nothing happens, here I leave the code of the 3 pages:
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 ListPrendas.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);
}
ListofPrendas.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>