• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem reading paramaters from a checkbox

 
Katie McCann
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a servlet that will be reading in data from an html form. The form will be passing in a number of hidden fields
as well as whatever choices the user selected from a list of checkboxes. Each checkbox will have the name "optionN" where N is
the option number, in sequential order based on their order in the form. Thus we may receive paramaters called
option3, option8, and option9, if 3,8, and 9 are the options the user selected.
We are trying to write a method to read ONLY the optionN paramaters into a String [] (EXCLUDING THE PARAMATERS
THAT WERE PASSED IN AS HIDDEN FIELDS, WHICH DO NOT HAVE THE NAME OF optionN). We have come up with 2 solutions, but
are trying to find something better. HEre are the 2:
1. Get an enumeration of the paramaters. Loop through the enumeration, and check if the name begins with "option". If so,
increment a counter to keep track of the number of "option" paramaters passed in. Once we have that, start a for loop
to request the paramatervalue for ("option" +i). Once it's found the correct # of options, break out of the loop.
What we don't like about this is the overhead in creating an enumeration instance.
2. THe second idea is to create a hard coded, arbitrary loop- ie. for (int i=0; i<300; ++i) to request the paramater
value for each ("option" + i). While this doesn't have the Enumeration overhead as in option 1 above, we have to use
an arbitrarily large number (such as 300) to be 100% sure that we haven't missed any options (the HTML page that
is calling this servlet is dynamically generated, and we have no way of knowing in advance how many possible
option choices there are)- so we're going to waste a lot of time looping up to 300 unnecessarily.
Does anyone have an idea for a "cleaner" way to do this?
 
milan doshi
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I would first suggest that you should really reconsider if it is necessary for u to have different names for your Checkboxes ( Sounds strange ? ) but thats where I think lies your probelem .
Consider as u have said that there are , say n number of options to be selected , i.e there r n number of checkboxes, than name them ALL , say productselected, like this:
pw.println("<input type=checkbox name=productselected value="+pcode+">");
:
:// the other code
:
:/*Please note that I am showing dynamic generation of
Checkboxes , i.e u never know how many options will be
created,baesd on the user query. Note the imp part here is to give DIFFERENT value to each checkbox. In this case I am ensuring that 'pcode' is different for each checkbox.( vide programing which I have not shown here )*/
You can then ver SIMPLY RETRIEVE ALL (i.e ALL THE SELECTED CHECKBOX VALUES ) simply by this line of command:
String purchase[]=req.getParameterValues("productselected");
and than depending on your need :
:
for(int i=0;i<purchase.length;i++)
{
:
This way is more useful espescially in Shopping Carts or related application wherein even the number of Checkboxes will be generated dynamically (i.e its number is not determined prior hand)
Hope this is of some help to you,
Milan
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. What Milan said is a good practice which I always use.
regds
maha anna
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic