user2268507
user2268507

Reputation:

Send dynamically created table information to java servlet - no javascript/jquery

I currently have the following in a jsp file:

<c:choose>
    <c:when test = "${fn:length(song.songInfo) > 0}">   
        <form action='cart' method='POST'>
            <table style="width:1000px" style="text-align:center">
                <tr>
                    <th>Song Title</th>
                    <th>Song Artist</th>
                    <th>Album Title</th>
                    <th>Genre</th>
                    <th>Publisher</th>
                    <th>Year</th>
                    <th>Price</th>
                    <th>Select ?</th>
                </tr>
                <c:forEach var="item" items="${song.songInfo}"> 
                    <c:set var="split" value="${fn:split(item,';')}" /> 
                    <tr>
                      <td>${split[0]}</td>
                      <td>${split[1]}</td> 
                      <td>${split[2]}</td>
                      <td>${split[3]}</td>
                      <td>${split[4]}</td>
                      <td>${split[5]}</td>
                      <td>${split[6]}</td>
                      <td>${split[7]}</td>
                      <td><input type="checkbox" name="songInfo" value="${split[0]} "></td>
                    </tr>   
            </c:forEach>
            </table>
            <input type="submit" value="Add to Cart"/>
        </form>

    </c:when>
    <c:otherwise>
        <p>No results found</p>
    </c:otherwise>
</c:choose>

I want to send the <td> items to a Java servlet IF the corresponding checkbox for the row that has been ticked.

I'm using strCheckBoxValue = request.getParameter("songInfo"); in my servlet but this will only retrieve one string.

Could someone suggest a way that I could send all the information to the servlet for any number of rows in the table WITHOUT using javascript/JQuery?

Thank you for your help.

Upvotes: 0

Views: 1095

Answers (1)

david brainerd
david brainerd

Reputation: 206

You can either give each TD a corresponding hidden input with a name, so that it will be submitted with the form:

<td>${split[0]}<input type='hidden' name='split0' value='${split[0]}' /></td>
<td>${split[1]}<input type='hidden' name='split1' value='${split[1]}' /></td>

Or just give the TDs visible inputs:

<td><input type='text' name='split0' value='${split[0]}' /></td>
<td><input type='text' name='split1' value='${split[1]}' /></td>

If you give each one a unique name (as I did above), then in your servlet:

String split0 = request.getParameter("split0"); 
String split1 = request.getParameter("split1");

If you give them all the same name, i.e. name='split':

String[] split = request.getParameterValues("split");

Upvotes: 2

Related Questions