3

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.

Community
  • 1
  • 1
Aikanáro
  • 867
  • 3
  • 20
  • 42

3 Answers3

3

your jsp should look somethin like this (using the servlet code you have posted)

first edit your servlet and include:

ArrayList<Book> shoppingCar = new ArrayList<Book>();
request.setAttribute("b", shoppingCar);//accsessed as ${b} in jsp

in your jsp you will have:-

     <form action="yourserlet" method="POST">
                <table>
                    <thead>
                        <tr>
                            <td width="10%">ISBN</td>
                            <td width="30%">TITLE</td>
                            <td width="30%">AUTHOR</td>
                            <td width="20%">SELECT</td>
                        </tr>
                    </thead>

                    <tbody>

        <c:forEach items="${b}" var="book">  
                 <tr>     
                   <td align="left"><input type="text" name="isbn<c:out value="${book.isbn}"/>"  disabled="true"/></td>                     
                     <td align="left"><input type="text" name="title<c:out value="${book.title}"/>"  disabled="true"/></td> 
                     <td align="left"><input type="text" name="author<c:out value="${book.author}"/>"  disabled="true"/></td> 
                     <td align="left"><input type="text" name="price<c:out value="${book.price}"/>"  disabled="true"/></td>
                     <td align="center">  
                        <input type="checkbox" name="checkboxgroup"   
                            value="c:out value="${book.tostring()}"/>"/>  
                     </td>  
                  </tr>  
             </c:forEach>   
      </tbody>
                </table>
            </form>

the you should possibly use jquery to enable or disable a field on checking a checkbox, i have disabled them by default.

check also:

jQuery - checkbox enable/disable

Getting all selected checkboxes values using ajax and jsp/servlets?

Community
  • 1
  • 1
mykey
  • 575
  • 2
  • 10
  • 24
2

In JSP Change

 <input type="checkbox" name="checkboxGroup" value="<%=Integer.toString(i)%>"/> 

OR

 <input type="checkbox" name="checkboxGroup" value="<%=i%>"/> 

will also work. You don't need to cast in to string values.

FYI: If you are planning to do something more. Better pass b.get(i).getID() kind of thing in parameters. Passing sequence may result incorrect data.

Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
  • A book have a title, an author, a price and a ISBN number. I use that to fill up the table, I can't pass the ISBN (that would be the ID) only. – Aikanáro Apr 13 '12 at 13:09
1

Your JSP code ..

<form method="POST" action="promoteSelected">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>*</th>
<th>AdmNo</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Check</th>
</tr>
</thead>
<tbody>
<%
if(studentList !=null){
    int scount = 1;
    for(Student stu : studentList){
    %>
<tr>
<td><%=scount%></td>
<td><%=stu.getAdmno()%></td>
<td><%=stu.getFirstname()%></td>
<td><%=stu.getLastname()%></td>
<td> 
 <div class="checkbox">     
<input type="hidden" name="studentId[]" value="<%=stu.getUuid()%>">     
<label><input type="checkbox" name="studentCheck[]">Check</label>   
</div>              
</td>
</tr>
<%
scount++;
} }
%>

</tbody>
</table>
<div class="form-actions">
<button type="submit" class="btn btn-primary">
<input type="hidden" name="schooluuid" value="<%=accountuuid%>">
      Promote
</button> 
</div>
</form>

Sevlet code ..

String[] studentCheck = {}; 
String[] studentId = {}; 

studentCheck = request.getParameterValues("studentCheck[]");
studentId = request.getParameterValues("studentId[]");
String schooluuid = StringUtils.trimToEmpty(request.getParameter("schooluuid"));

for(String str : studentCheck){
   System.out.println("studentCheck " + str);
}

for(String str : studentId){
    System.out.println("studentId " + str);
}
 System.out.println("schooluuid " + schooluuid);