0

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.

1
  • without javascript? Only thing I can think of is by putting it ina form and posting it to the server and do all the checks serverside. Ie check if the checkbox is ticked etc. Commented Apr 9, 2014 at 1:55

1 Answer 1

2

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");
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks David. But how do I check that the checkbox in each row has been ticked? And also, how do I get subsequent rows? Thanks for your help.
Oh right.. yes I got that. Sorry, what I meant was, how do I obtain the information from other rows? Because I am creating these rows dynamically, how would I select the 2nd or 3rd o 5th row for example.
I would give the rows in the various fields a rownum in the name. <input name='variablex_row1' /> In the servlet you can do a for loop on a variable i with request.getParameter("variablex_row"+i);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.