Solved

get all checked parameters in the servlet

Posted on 2006-11-28
5
201 Views
Last Modified: 2010-04-01
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("checkIssue"+i);
               
                System.out.println("check value:------------ : "+check);
               
               
                if (check.equals("issue"+i)){
                   
                 System.out.println("Enter after check if");  
               
                 int id  = Integer.parseInt(request.getParameter("index"+i));
                System.out.println("index is:"+i);
               
                String storekeeper = request.getParameter("userSession");
                System.out.println("userSession is: "+storekeeper);
               
                String empUsername = request.getParameter("username"+i);
                System.out.println("emp username is::"+empUsername);
               
                System.out.println("model is:"+i);
               
                String sn = request.getParameter("ItemSNs"+i);
                System.out.println("item sn is::"+sn);
               
                String cat = request.getParameter("request"+i);
               
                System.out.println("cat:-----"+cat);
               
               
               
                IssuedItem issuedItem = new IssuedItem();
               
                issuedItem.setRecievedTo(empUsername);
                issuedItem.setSerialNumber(sn);
                issuedItem.setCategory(cat);
                issuedItem.setIssuedBy(storekeeper);
               
                int isIssued = c.issueItem(issuedItem);
               
               
             
               
       
                }// end of if
 
            i++;
           
            }// end of while
           
        }// end of try
               
   catch(Exception e){
       
            System.out.println("Exception runs in action issue item:---- "+e.getMessage());
           
            if(request.getParameter("index"+i)== null){
               
                flag = false;
               
                System.out.println("after while");
       
   
    }// end of if null;    
0
Comment
Question by:Sh_Rashed
  • 3
5 Comments
 
LVL 9

Expert Comment

by:owenli27
ID: 18028366
the second row checkbox return null when you call  String check = request.getParameter("checkIssue1");
So change:   if (check.equals("issue"+i)) {...
To:   if (check != null && check.equals("issue"+i)) {...
0
 
LVL 9

Expert Comment

by:owenli27
ID: 18028442
Your while (flag){} loop will be infinite-loop.  You should change it to for-loop based on how many checkboxes you have.
0
 

Author Comment

by:Sh_Rashed
ID: 18028643
I don't know How many checkboxs I have, since the table generated according to the database records .
0
 
LVL 9

Expert Comment

by:owenli27
ID: 18028990
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("index"+i) == null)
       break;
   
     String check = request.getParameter("checkIssue"+i);
     System.out.println("check value:------------ : "+check);

     if (check != null && check.equals("issue"+i)) {...
     .....
0
 
LVL 5

Accepted Solution

by:
dasmaer earned 500 total points
ID: 18031807
A small cheat I used for something like this was a javascript counter declared at the top of the generated HTML.  When a new check box is drawn by the JSP, you add a <script></script> that increments the counter each time.  If you then wanted to be super clever you could write a small javascript that'll check if all the boxes have been ticked (or whatever you;re condition is), so that you can do less checking on the server side.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to group array of objects ? 5 61
Looking for some programs to resell or buy source codes 1 75
excpetion with multiple catch 11 113
equalIsNot  challenge 43 134
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now