File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes selecting multiple rows to update value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "selecting multiple rows to update value" Watch "selecting multiple rows to update value" New topic
Author

selecting multiple rows to update value

Lijo Kuriala
Greenhorn

Joined: Jul 15, 2008
Posts: 4
Hi everyone,
I'm new to JSP, and trying to do a shopping cart problem.
In my jsp page i'm displaying a list of books row-wise, along with a text field to enter the quantity. At the end of the list, i have the submit button. On submitting, i needed to know which all rows have been selected, and the corresponding quantity entered.
Please help with the logic of how to go about this. My code is rather messy now, will post it soon.
Thankyou
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

That greatly depends upon what you mean by "selected".


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Lijo Kuriala
Greenhorn

Joined: Jul 15, 2008
Posts: 4
By selected, i mean that the row is selected by checking the checkbox. Its like a typical e-mail inbox. You can selected messages to delete by selecting the checkboxes against those you want.

My jsp file is as follows...

<blockquote>code:
<pre name="code" class="core">
<%@ page session="true" language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="java.sql.*,com.ustr.shoppingcart.*,java.util.*,java.io.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<jsp:useBean id="test" class="com.ustr.shoppingcart.DBconnect" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>List of all Books</title>
</head>
<h1 align='center'><marquee BEHAVIOR=ALTERNATE bgcolor=black><font color="white"> Asian Books </marquee></h1>
<body>
<form name=myname method=post action="/Shoppingcart/Test">
<table cellspacing=20 cellpadding=3>
<tr><td></td>
<td><b><u>Bookid</u></b></td>
<td><b><u>Bookname</u></b></td>
<td><b><u>Author</u></b></td>
<td><b><u>Price</u></b></td>
<td><b><u>Quantity</u></b></td>
<td><b><u>Order</u></b></td>
<td></td>
</tr>
<%
try
{
ResultSet rs = test.ExecuteQuery("select * from booklist");
while(rs.next()){
%>
<tr>
<td><input type="checkbox" name="bt" value="<%= rs.getString("bookid")%>" ></td>
<td><h3><%= rs.getString("bookid") %></td>
<td><h3><%= rs.getString("bookname") %></td>
<td><h3><%=rs.getString("author") %></td>
<td><h3><%=rs.getString("price") %></td>
<td><h3><%=rs.getString("quantity") %></td>
<td><input type="text" name="quantity"></td>
</tr>
<%
}//end while
%>
</table>
<input type=submit name=submit value=Submit>
<%}catch(Exception E)
{
System.out.println(E);
}
%>
</body>
</html>
</pre>
</blockquote>

Thanks,
Lijo
Khirod Patra
Greenhorn

Joined: Mar 27, 2008
Posts: 23
Hi Lijo
According to your code i.e
<tr>
<td><input type="checkbox" name="bt" value="<%= rs.getString("bookid")%>" ></td>
<td><h3><%= rs.getString("bookid") %></td>
<td><h3><%= rs.getString("bookname") %></td>
<td><h3><%=rs.getString("author") %></td>
<td><h3><%=rs.getString("price") %></td>
<td><h3><%=rs.getString("quantity") %></td>
<td><input type="text" name="quantity"></td>
</tr>

it populate all the values Above, and when you click on the submit button all the values will transfer to your Action Page, i,e shopping/test.All these values can be pulled using getParameter Method.

Let me know If any problem with this solution

Cheers
Khirod chandra Patra
Lijo Kuriala
Greenhorn

Joined: Jul 15, 2008
Posts: 4
My page looks roughly like below

Select Bookid Title Author Price QuantityToBuy
□ b0001 Godplayer Sheldon 255.50 -----
□ b0002 NakedFace Sheldon 210.00 -----
□ b0003 Client Grisham 300.00 -----
(SubmitButton)

-------------------------------------------------------------
I've to selected the books by checking the corr checkboxes, then enter the quantity. On submitting i want to get info of the selected rows, bookId and quantity

http://mobilekaps.blogspot.com/2008/07/test.html This link has a screenshot.

Thanks,
Lijo

[ July 16, 2008: Message edited by: Lijo Kuriala ]
[ July 16, 2008: Message edited by: Lijo Kuriala ]
Lijo Kuriala
Greenhorn

Joined: Jul 15, 2008
Posts: 4
Everyone,
I've got a solution to my problem. I use a counter and append the counter to the name of the required items, then use them in the action page.


<blockquote>code:
<pre name="code" class="core"> <%
try{
ResultSet bookList = getBooks.ExecuteQuery("select * from books");
int i=0;
while(bookList.next()){
i++;
%>
<tr>
<td><input type="checkbox" name="selectBook+<%=i%>" value=<%= bookList.getString("bookid") %>></td>
<td><%= bookList.getString("bookid") %></td>
<td><%= bookList.getString("author") %></td>
<td><%= bookList.getString("title") %></td>
<td><%= bookList.getString("price") %></td>
<td><%= bookList.getString("quantity") %></td>
<td><input type="text" name="numNeeded+<%=i%>" ></td>
</tr>
<%
}//end while
}catch(SQLException e){
System.out.println(e.getMessage());
}
%>
</pre>
</blockquote>

Thankyou to all those replied....
Regards,
Lijo
Bhushan Ahirkar
Greenhorn

Joined: May 22, 2008
Posts: 1
Hi Lijo,

Can you please illustrate how to use counter appended name in the action page .
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
Hi both!

As you said you are new to JSP you should know that the queries and other operations should never be done in the JSP, you can use a bean or a tag or a function or whatever but do not do it in the JSP.

Lijo, what are you using your "test" bean for? You call it but you don't use it (or I cannot see where do you use it).

sanat meher
Greenhorn

Joined: Nov 06, 2008
Posts: 18
hi..

how can i get those check box value in the target servlet/jsp using request.getParameter ?

Regards,
Sanat
Archana Honnavalli
Ranch Hand

Joined: Feb 26, 2008
Posts: 39
Hi Alberato,
I agree with you that in this case "test" bean is not at all used.

But in scenarios where bean is not implemented
(Assuming an application with just jsp pages)
Is it ideal to use the text box content with the counter as described by Lijo?
Or if there is another ideal way please tell me.

Hi Lijo,
Can you print your code where value of textbox is obtained using counter logic?
I mean getting value from this textbox.
<td><input type="text" name="numNeeded+<%=i%>" ></td>

Thanks,
Archana
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
Hi there!

I don't understand why to have an application without Servlets and classes and so on. Is there any particular reason?

To get the results from checkboxes you can use request.getParameterValues(String name); which returns a String[] I don't know if it's the ideal but normally if the API provides tools I think is because is good to use them
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Archana Honnavalli wrote:(Assuming an application with just jsp pages)

Why? That would be counter to all best practices.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: selecting multiple rows to update value