简体   繁体   中英

Retrieving a specific value in an array from JSP to Servlet

This code creates a table out of an array of strings stored inside a session. I then have a submit button on the end of each row which when clicked should store the Stock number of reach row (placed in the 0th column of each row) and send it to the servlet. The table is displaying properly, however I cannot retrieve the Stock# column for each individual row. Where did I go wrong?

 <table>  
  <tr>  

      <th>Stock#</th>  <th>Name</th>  <th>Price</th>  <th>Description</th>  

  </tr>  
  <c:forEach items="${BooksTable}" begin="0" var="row">  
    <tr>  
      <c:forEach items="${row}" var="cell"> 

        <td>${cell} <BR>  <BR>  </td>  
      </c:forEach>  
      <c:set value="${$row.count}" scope="session" var="itemID" />
       <td><FORM ACTION="CartAccess">

         <INPUT TYPE ="HIDDEN" NAME="item" VALUE="${BooksTable[1][0]}">

        <INPUT TYPE="SUBMIT" NAME="check" VALUE="Add"> </FORM></td>   
    </tr>  
  </c:forEach>  
</table>  

You can add a test to check If the index == 0 , if it is then c:set a variable stockValue and then use it to create Hidden field.

   <c:forEach items="${BooksTable}" begin="0" var="row">  
    <tr>  
      <c:forEach items="${row}" var="cell" varStatus="rowIndex"> 

        <c:if test="${rowIndex.index == 0}" >
           <c:set var="stockValue" value="${cell}" scope="page" />
       </c:if>

        <td>${cell} <BR>  <BR>  </td>  
      </c:forEach>  

      <c:set value="${$row.count}" scope="session" var="itemID" />
       <td><FORM ACTION="CartAccess">
         <INPUT TYPE ="HIDDEN" NAME="item" VALUE="${stockValue}">
        <INPUT TYPE="SUBMIT" NAME="check" VALUE="Add"> </FORM></td>   
    </tr>  
  </c:forEach> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM