我有下一個名爲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