I have the next jsp file called Bookstore.jsp
, in which I filled up a table with data from a database.
<%
ArrayList<Book> b = new ArrayList<Book>();
b = SqlSentencesList.showCatalog(); // this method returns an arrayList with all books
%>
<form method="get" action="ShoppingCarController">
<table border="2">
<tr>
<th>ISBN</th>
<th>Title</th>
<th>Author</th>
<th>Price</th>
<th>Select</th>
</tr>
<%for(int i=0; i<l.size();i++){%>
<tr>
<td> <%out.print(b.get(i).getIsbn());%> </td>
<td> <%out.print(b.get(i).getTitle());%> </td>
<td> <%out.print(b.get(i).getAuthor());%> </td>
<td> <%out.print(b.get(i).getPrice());%> </td>
<th> <input type="checkbox" name="checkboxGroup" value="<%Integer.toString(i);%>"/> </th>
</tr>
<% } %>
</table>
<input type="submit" value="Add to shopping car"/>
</form>
Now, I need the same book data (ISBN, title, author and price) in the Servlet, but just from the ones selected.
This's my doGet method from ShoppingCarController servlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ArrayList<Book> shoppingCar = new ArrayList<Book>();
String[] values = request.getParameterValues("checkboxGroup");
for(int i=0; i<values.length; i++) {
System.out.println(values[i]);
}
}
I tried to printed it to see what I'm getting, but nothing shows up in the console.
I was looking at this similar case: How to pass data from selected rows using checkboxes from JSP to the server and I think my problem is with the value
attributes, but I don't know the syntax used in that question, don't understand that for each
and the <c:out
tags; in short, I don't know how to adapt my code to get it work.
Someone give me a hand with that.