从jsp向servlet发送数据数组

时间:2021-07-06 11:48:42

im pretty newbie about JSTL, JSP and alike, but im building part of a program in which i need to send a collection of data to a servlet, so i can insert it on the DB

即时通讯关于JSTL,JSP等相关的新手,但是我构建了一个程序的一部分,我需要将一组数据发送到servlet,所以我可以将它插入到数据库中

the code is the following

代码如下

" target="imain"> Inventario

“target =”imain“> Inventario

Fecha

        <input type="text" id="fecha_web" name="fecha_web">

      </p>



    <p>recepcionista
    <label for="rut_recepcionista"></label>
    <label for="select"></label>



    <label for="rut_coordinador"> coordinador</label>
    <label for="cmb_cord"></label>
    <select name="cmb_cord" id="cmb_cord">
                <c:forEach var="fila_cord" items="${listaCoordinador}">
                                <option value="<c:out value="${fila_cord.run}" />" >
                                    <c:out value="${fila_cord.nombres}" />
                                </option>
                                </c:forEach>
    </select>
    </p>
              <table width="428" border="1">
                <tr>
                  <th width="127" scope="col">Material</th>
                  <th width="168" scope="col">Cantidad</th>
                  <th width="111" scope="col">Cantidad previa</th>
                </tr>

                <c:forEach var="fila_m" items="${listaMat}" varStatus="status">
            <tr>
                <td><c:out value="${fila_m.nombre_material}" /></td>
                <td><input type="text" name="total_actual" id="total_actual" 
                value="<c:out value="${fila_m.cant_material_total}"/>" /></td>
                <td> <input name="cant_totalpub" type="text" id="cant_totalpub" value="<c:out value="${fila_m.cant_material_total}" />" disabled/>
                    <input name="cant_totalhid" type="text" id="cant_totalhid" value="<c:out value="${fila_m.cant_material_total}" />" hidden/>
                <input name="mathid" id="mathid" type="text" size="1" value="<c:out value="${fila_m.cod_material}"/>" hidden></td>

            </tr>
        </c:forEach>
              </table>
              <p>
                <label>guardar
                  <input type="submit" name="guardar" id="guardar" value="Submit" />
                  <br />
                </label>
              </p>

        </form> 
    </body>
    </html>

that is the .jsp and the servlet:

那是.jsp和servlet:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    Connection conexion = null;
    try {
        /* TODO output your page here. You may use following sample code. */
        conexion = ds.getConnection();
        InventarioDAO dao = new InventarioDAO();
        dao.setCon(conexion);
        Inventario inventario= new Inventario();

        String fecha = request.getParameter("fecha_web");
        int Rut_coordinador = Integer.parseInt(request.getParameter("rut_cord_web"));
        int Cod_material = Integer.parseInt(request.getParameter("cod_material_web"));
        int cant_actual = Integer.parseInt(request.getParameter("cant_actual_web"));
        int cant_total = Integer.parseInt(request.getParameter("cant_total_web"));


        inventario.setCant_material_actual(cant_actual);
        inventario.setCant_material_total(cant_actual);
        inventario.setCod_material(Cod_material);
        inventario.setFecha(fecha);
        inventario.setRut_coordinador(Rut_coordinador);

        dao.guardarInventario(inventario);
        //revisar bien donde tengo que enviarlo
   request.getRequestDispatcher("/ListarHGServlet").forward(request, response);
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            conexion.close();
        } catch (Exception noGestionar) {

        }
    }
}

the problem is that actually works, but it just saves the operations realized to the first on the list, meanwhile the rest is ignored, i want to know how i can realize that operation for every item on the list, i've been searching but kinda i don't seem to find what i need.

问题是实际上有效,但它只是将实现的操作保存到列表中的第一个,同时其余部分被忽略,我想知道如何实现列表中每个项目的操作,我一直在搜索但是有点我似乎没有找到我需要的东西。

P.S: i don't talk english as first language, so sorry for any problem caused by that.

P.S:我不会说英语作为第一语言,所以对于由此引起的任何问题都很抱歉。

1 个解决方案

#1


0  

 String fecha_web = request.getParameter("fecha_web");

//for multiple form elements with same name.

//用于具有相同名称的多个表单元素。

   String total_actuals[] = request.getParameterValues("total_actual");
   String cant_totalpubs[] = request.getParameterValues("cant_totalpub");
   String cant_totalhids[] = request.getParameterValues("cant_totalhid");
   String mathids[] = request.getParameterValues("mathid");
   if(mathids!=null)
   {
       for(int i=0;i<mathids.length;i++)
       {
           int total_actual = Integer.parseInt(total_actuals[i]);
           int cant_totalpub = Integer.parseInt(cant_totalpubs[i]);
           int mathid = Integer.parseInt(mathids[i]);
       }
   }

#1


0  

 String fecha_web = request.getParameter("fecha_web");

//for multiple form elements with same name.

//用于具有相同名称的多个表单元素。

   String total_actuals[] = request.getParameterValues("total_actual");
   String cant_totalpubs[] = request.getParameterValues("cant_totalpub");
   String cant_totalhids[] = request.getParameterValues("cant_totalhid");
   String mathids[] = request.getParameterValues("mathid");
   if(mathids!=null)
   {
       for(int i=0;i<mathids.length;i++)
       {
           int total_actual = Integer.parseInt(total_actuals[i]);
           int cant_totalpub = Integer.parseInt(cant_totalpubs[i]);
           int mathid = Integer.parseInt(mathids[i]);
       }
   }