Sh_Rashed
asked on
get all checked parameters in the servlet
Hi Experts,
I have table in a form, so each row in the table is input (type hidden) and has check box option.
When I have checked two items ( in the first and third row) and submitted , the Servlet takes the first index insert it then it gives me null Exception
so how I can go over all the table rows and see it is checked or not?
This is my Servlet method:
boolean flag = true;
int i = 0;
int counter = 0;
Controller c = new Controller();
Vector result = new Vector();
try {
while (flag){
String check = request.getParameter("chec kIssue"+i) ;
System.out.println("check value:------------ : "+check);
if (check.equals("issue"+i)){
System.out.println("Enter after check if");
int id = Integer.parseInt(request.g etParamete r("index"+ i));
System.out.println("index is:"+i);
String storekeeper = request.getParameter("user Session");
System.out.println("userSe ssion is: "+storekeeper);
String empUsername = request.getParameter("user name"+i);
System.out.println("emp username is::"+empUsername);
System.out.println("model is:"+i);
String sn = request.getParameter("Item SNs"+i);
System.out.println("item sn is::"+sn);
String cat = request.getParameter("requ est"+i);
System.out.println("cat:-- ---"+cat);
IssuedItem issuedItem = new IssuedItem();
issuedItem.setRecievedTo(e mpUsername );
issuedItem.setSerialNumber (sn);
issuedItem.setCategory(cat );
issuedItem.setIssuedBy(sto rekeeper);
int isIssued = c.issueItem(issuedItem);
}// end of if
i++;
}// end of while
}// end of try
catch(Exception e){
System.out.println("Except ion runs in action issue item:---- "+e.getMessage());
if(request.getParameter("i ndex"+i)== null){
flag = false;
System.out.println("after while");
}// end of if null;
I have table in a form, so each row in the table is input (type hidden) and has check box option.
When I have checked two items ( in the first and third row) and submitted , the Servlet takes the first index insert it then it gives me null Exception
so how I can go over all the table rows and see it is checked or not?
This is my Servlet method:
boolean flag = true;
int i = 0;
int counter = 0;
Controller c = new Controller();
Vector result = new Vector();
try {
while (flag){
String check = request.getParameter("chec
System.out.println("check value:------------ : "+check);
if (check.equals("issue"+i)){
System.out.println("Enter after check if");
int id = Integer.parseInt(request.g
System.out.println("index is:"+i);
String storekeeper = request.getParameter("user
System.out.println("userSe
String empUsername = request.getParameter("user
System.out.println("emp username is::"+empUsername);
System.out.println("model is:"+i);
String sn = request.getParameter("Item
System.out.println("item sn is::"+sn);
String cat = request.getParameter("requ
System.out.println("cat:--
IssuedItem issuedItem = new IssuedItem();
issuedItem.setRecievedTo(e
issuedItem.setSerialNumber
issuedItem.setCategory(cat
issuedItem.setIssuedBy(sto
int isIssued = c.issueItem(issuedItem);
}// end of if
i++;
}// end of while
}// end of try
catch(Exception e){
System.out.println("Except
if(request.getParameter("i
flag = false;
System.out.println("after while");
}// end of if null;
Your while (flag){} loop will be infinite-loop. You should change it to for-loop based on how many checkboxes you have.
ASKER
I don't know How many checkboxs I have, since the table generated according to the database records .
We can fix this problem by check if the hidden input box existing ot not if there always be a hidden input box with a checkbox together.
while (flag) {
if( request.getParameter("inde x"+i) == null)
break;
String check = request.getParameter("chec kIssue"+i) ;
System.out.println("check value:------------ : "+check);
if (check != null && check.equals("issue"+i)) {...
.....
while (flag) {
if( request.getParameter("inde
break;
String check = request.getParameter("chec
System.out.println("check value:------------ : "+check);
if (check != null && check.equals("issue"+i)) {...
.....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
So change: if (check.equals("issue"+i)) {...
To: if (check != null && check.equals("issue"+i)) {...