Send arraylist from form jsp to servlet and show results in another jsp

1

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>
    
asked by Jordan Blake Told 12.09.2017 в 17:08
source

1 answer

2

I'll modify a couple of things ... and I'll put what is not right.

Servlet:

  • You have in all request.getParameter("Codigo") , request.getParameter("Marca") that is, they all start in capital letters, instead in your form, in name of each input you have it in lowercase, therefore you will never get values
  • In the case of select the value that you will get is that you have the attribute value of option , therefore you do not need to do .charAt(0) to get the first letter, then in the value you have written what you need, with the request.getParameter("talla") as in the rest of the input , you will get the value you want.
  • I understand that Biblioteca is a class and that prendas is a variable List of type static of the class, as good practice try to encapsulate your code, declare all your variables private and define your getter and its setter, as you have done well in class Prenda . Your Servlet , will not die after doing a redirect, the variables declared globally will be kept alive there.
  • In the for you are going through all your garments but when doing request.setAttribute("codigo",codigo) , you are replacing the value with the element of each loop, therefore at the end you would only have the value of the last element of the loop. in the setAttribute you can put it from a String to the object that you want it (Like all your list of garments).

    Biblioteca biblioteca=new Biblioteca();
    
    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"));
                    ropa.setTalla(request.getParameter("talla"));
                    //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.getPrendas().add(ropa);
                    //                  
                    request.setAttribute("prendas", biblioteca.getPrendas());
    
    
                    RequestDispatcher despachador = request.getRequestDispatcher("ListadoDePrendas.jsp");
                    despachador.forward(request, response);
        }
    

Listings.jsp

<%
 List<Prendas> lista=request.getAttribute("prendas");
 Iterator<Prendas> itPrendas=lista.iterator();
%>
<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>
               <%while(itPrendas.hasNext()){
                  Prenda prenda=it.next();%>
                <tr>
                   <td><%= prenda.getCodigo() %></td>
                   <td><%= prenda.getNombre() %></td>
                   <td><%= prenda.getMarca() %></td>
                   <td><%= prenda.getTalla() %></td>
                   <td><%= prenda.getFecha() %></td>
                   <td><%= prenda.getStock() %></td>
                   <td><%= prenda.getTemporada() %></td>
                   <td></td>
                </tr>
               <%}%>                
                </tbody>           
            </table>
                <a href="index.jsp">Volver</a>  
        </body>   
    </html>

Version with JSTL

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <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>
                   <c:forEach items="${prendas}" var="prenda">
                    <tr>
                       <td>${prenda.getCodigo()}</td>
                       <td>${prenda.getNombre()}</td>
                       <td>${prenda.getMarca()}</td>
                       <td>${prenda.getTalla()}</td>
                       <td>${prenda.getFecha()}</td>
                       <td>${prenda.getStock()}</td>
                       <td>${prenda.getTemporada()}</td>
                       <td></td>
                    </tr>
                   </c:forEach>                
                    </tbody>           
                </table>
                    <a href="index.jsp">Volver</a>  
            </body>   
        </html>
    
answered by 12.09.2017 / 17:39
source