Solved

Retrieving multiple checkboxes from a form using OReilly.servlet

Posted on 2004-08-19
7
411 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 27

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 500 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
Technology Partners: 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
immutable object concept 5 132
if statement not resolving in my code 5 55
maven set up 2 187
by zero exception 10 69
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 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