Solved

Retrieving multiple checkboxes from a form using OReilly.servlet

Posted on 2004-08-19
7
405 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
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.

 
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

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
Run the project 5 87
difference between member and local variables. 29 126
struts hibernate integration example 28 124
how to hide hide popup page refresh second time? 10 112
Cloud-based technologies and services will continue to grow in popularity in 2017 thanks to the simple, scalable and cost-effective solutions they deliver. Here are three areas where cloud adoption is poised to really take off.
Knowledge base software has turned out to be a quite reliable method for storing information, promoting collaborative work and for sharing valuable input and solutions.However, some organizations are trying to develop a knowledge base that works wit…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

26 Experts available now in Live!

Get 1:1 Help Now