Solved

Retrieving multiple checkboxes from a form using OReilly.servlet

Posted on 2004-08-19
7
401 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 92

Expert Comment

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

Expert Comment

by:sompol_kiatkamolchai
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

HOW TO: Install and Configure VMware vSphere Hypervisor 6.5 (ESXi 6.5), Step by Step Tutorial with screenshots. From Download, Checking Media, to Completed Installation.
HOW TO: Connect to the VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere (HTML5 Web) Host Client 6.5, and perform a simple configuration task of adding a new VMFS 6 datastore.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

16 Experts available now in Live!

Get 1:1 Help Now