Click to See Complete Forum and Search --> : servlet problem with Checkbox
hensome2004
01-13-2005, 10:29 PM
I have a simple problem with my servlet:
I have a JSP page that dynamically lists records in a table from my database, based on the user input search criteria. At the left hand side of each row in the table, there are checkboxes for the user to select a particular record that he/she wants to do manipulation. Since the records are dynamically listed in the JSP, I don't know how to get the checkbox values in my servlet. I am confused whether I should use boolean array or String array to store the value got from the statement:
request.getAttribute("select");
In the JSP file, I defined:
<form action="servelet" method=post name="myform">
<table align=center>
<% while (i.hasNext()) { %>
<tr>
<td><input type=checkbox name="select"/></td>
<td>${record.name}</td>
<td>${record.age}</td>
<td>${record.sex}</td>
</tr>
<% } %>
</table>
</form>
Then how should I get which record the user has selected?
boolean[] select = (boolean) request.getAttribute("select");
OR
String[] select = (String) request.getAttribute("select");
Please help.
Khalid Ali
01-14-2005, 12:24 AM
the first solution that come to mind is that name every checkbox the record id/primary key etc for that particular record
for e.g if the current records unique id is 11112 then when you create the check box name it as such
<input type=checkbox name="11112"/>
now you just have to get this name and then retrive that record from the db...its just one approach there can be multiple options
hensome2004
01-14-2005, 12:31 AM
There is no unique ID field in the database. The records in the database just contain name, age, sex, date of birth, etc. Besides, the user may add new records to the database using this web application. And when this JSP runs, it shows ONLY the user selected records. For example, when the user selects to show ONLY the people who are of age 18, then this JSP is going to list all people of age 18. Then I would like to provide a checkbox in front of each of these records and let the user to select which particular person of age 18 would the user like to delete. If the user chooses the first, the third and the fourth record listed and click the delete button to submit the form, then these records gonna be deleted.
So what is the proper way to write the JSP and Servlet to fulfill this requirement?
Many Thanks.
Khalid Ali
01-14-2005, 01:31 AM
Originally posted by hensome2004
There is no unique ID field in the database. ..
Thats a bad design, there must always be a unique id for each record in the database with which that record is linked. Update your db and add a new column name it something like
user_id or person_id etc
then use this id to name the check boxes
hensome2004
01-14-2005, 02:23 AM
Oh yes, I understand that is a bad design. But this is the coursework that I have to do and I am not allowed to alter any of the codes originally given. Is there any workraround to this problem?
And suppose that the database does have a unique ID field. How could I dynamically name the checkboxes? The record list should be different, for example, when a user selects to show records of age 18 rather than those of age 19.
Thanks.
Khalid Ali
01-14-2005, 09:48 AM
Here is the logic that you want to use.
1. query the database and get everything (select * from table_name)
2. now going thru the resultset create your html code
3. with each table row for each ecord e.g.
<td><input type=checkbox name=${record.userId}/></td>
<td>${record.name}</td>
<td>${record.age}</td>
<td>${record.sex}</td>
follow this pattern
now when user selects a check box and submits the page to server
you get all the parameters and whichever userId is selected you get that and then run a query to db
select * from table_name where user_id=selected_user_id_from_the_page
this will bring back the account user want to work with...
hope this helps
hensome2004
01-14-2005, 10:33 AM
Thank you very much for the logic. I know it would work well under the assumption that the database contain a unique key named UserID or whatsoever. But now my database contains no unique key and I am not allowed to change this. Is there any work arounds?
I just think how could I get an array of boolean values of whether the checkboxes are checked. I know in JavaScript I could write:
var checked[i] = myform.userChoice[i].checked;
But how could I get this in a servlet? I tried the following statement but it fails:
boolean[] userChoice = (boolean[]) request.getAttribute("userChoice");
If only I could get this array of boolean of which the checkboxes are checked, I am able to write loops that map each checkbox with its coresponding database record.
PS: Assume the checkbox is named "userChoice".
Khalid Ali
01-14-2005, 10:40 AM
Originally posted by hensome2004
I just think how could I get an array of boolean values of whether the checkboxes are checked
if you just want a checkbox to be sent to servlet then just name all of them "userChoice" and then in the servlet you will get only the check boxes that are checked by user.
In the servlet
String[] userChoices = request.getParamterValues("userCHoice");
will give u an array of all the check boxes that were selected.
In my opinion problem will still remain that which checkbox is linked to which record in the database.
I think you will have to change your logic and name all the check boxes uniquely, so that when user clicks on one you add that chekc box related data to a hidden field and name it almost the same as check box
ray326
01-14-2005, 05:01 PM
Name the checkbox "${record.name}:${record.age}:${record.sex}" then let the servlet reconstitute the only thing you've got for a key.