Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Retrieving multiple checkboxes from a form using OReilly.servlet

Posted on 2004-08-19
7
Medium Priority
?
440 Views
Last Modified: 2012-06-21
I have a situation that I am stuck on.
I have a JSP that looks into my Db and then decides how many checkboxes to put on the form based on the query (one checkbox for each database row in my category table).
The user can then check whichever ones s/he wants to and then submit the form.
For various reasons, I need to use the oReilly.servlet package.
I have named my checkboxes as follows, so i actually have uniqu checkbox names.

<input name=cat_check"+rs.getString("id")+" type=checkbox value=1>
*Note: I even have a hidden field called "$cat_count" that stores how many checkboxes I have

But now I need to go to the JSP that handles all of the form submissions, and I do not know how to get the checkbox names back.  My code looks lik this...

MultipartRequest multi = new MultipartRequest(request,"d:/WebSphere/blah blah/"+$frm_dest, 50 * 1024, new com.oreilly.servlet.multipart.DefaultFileRenamePolicy());
Enumeration params = multi.getParameterNames();
String $cat_count ="0";
while (params.hasMoreElements()) {
   if($parm_name.equals("cat_count")){ $cat_count = $parm_value; }
}

//Check the parameters for the dynamic check boxes
if($cat_count == null || $cat_count.equals("0") || $cat_count.equals("")){
      //Do nothing for now
}else{
      //$cat_count2 = $cat_count;
      Enumeration params2 = multi.getParameterNames();
      while (params2.hasMoreElements()) {
            // I was going to try some sort of while loop here to see if I could
            // get the names out and compare them to $parm_name
      }
}

I thought I could read in the $cat_count and then do some sort of while loop to check to see if the $parm_name was equal to the checkbox names and then get the $parm_value in the second while loop with parms2.

Somehow I figure I am being REALLY inefficient.

Can someone show me some code on how I should handle this issue properly?
I would really appreciate it - my brain is fried!

Thanks,
-MD
0
Comment
Question by:mderbin
7 Comments
 
LVL 28

Expert Comment

by:rrz
ID: 11848642
Here is one way.

Enumeration params = multi.getParameterNames();
String $cat_count ="0";
String param;
HashMap boxes = new HashMap();
while (params.hasMoreElements()) {
   param = (String)params.nextElement();
   if("cat_count".equals(param)){ $cat_count = param;}
    else{boxes.put(param, multi.getParameter(param);}
}
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 11849041
String prefix = "cat_check";
while (params2.hasMoreElements()) {
   String name = (String) params2.nextElement();
   if (name.startsWith(prefix))
   {
      int id = Integer.parseInt(name.substring(prefix.length()));
      Object value = multi.getParameter(name);
      // do what u need to do
   }
}
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 11849713
why don't you name the checkbox element with the same name like this

<input type="checkbox" name="cat_check" value="<%=rs.getString("id")%>">

and when you submit this form to jsp/servlet, you can get the value as array

<%
String[] cat_check = request.getParameterValues("cat_check");
%>

I think it would be smarter.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 11849779
don't see any real purpose for the cat_count parameter.
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 11849795
object.

agree with you. I think it does not need to send the count value because we can get it from .length of array

<%
String[] cat_check = request.getParameterValues("cat_check");
out.println(cat_check.length);
%>
0
 

Author Comment

by:mderbin
ID: 11854111
That code worked perfectly.  Thank you very much!
I don't know how many more hours I would have wasted on that.
I guess the only purpose for the cat_count is that the page gets called from multiple different places, and if trhere are no checkboxes on those pages, I can save the server a little processing work.
Thanks!
-MD
0
 
LVL 92

Expert Comment

by:objects
ID: 11856885
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Following on from our article on "The Murky World of Consent and opt in", we thought we would issue some helpful guidance, not only on consent itself but knowing what information you are capturing, what you are doing with this data and how you can p…
Do online retailers and e-commerce sites even need to bother with the season? Is it lucrative to update and prepare your site for Black Friday and Cyber Monday shoppers? We'll find out now.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

824 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