我有下一个名为Bookstore.jsp
的jsp文件,其中我使用数据库中的数据填充表。使用Servlets和JSP从html表中获取选定的行
<%
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>
现在,我需要在Servlet同一本书的数据(ISBN,书名,作者和价格),但只是从选定的人。
这是从ShoppingCarController我的servlet的doGet方法:
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]);
}
}
我试图打印它,看看有什么我得到,但没有在控制台中显示出来。
我一直在寻找这种类似的案例:How to pass data from selected rows using checkboxes from JSP to the server,我想我的问题是与value
属性,但我不知道这个问题所使用的语法,不明白for each
和<c:out
标签;总之,我不知道如何调整我的代码才能使它工作。
有人给我一个这样的手。
一本书有一个标题,一个作者,一个价格和一个ISBN号码。我用它来填满表格,我无法通过ISBN(这将是ID)。 – 2012-04-13 13:09:46
我已根据您的servlet代码段进行了回答。 – 2012-04-13 14:15:18