Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Retrieving multiple checkboxes from a form using OReilly.servlet

Posted on 2004-08-19
7
Medium Priority
?
432 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
What we learned in Webroot's webinar on multi-vector protection.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

670 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