checkbox selection....

I'm dynamically creating a series of checkboxes based on a user's profile (done through a sql query) shown below:

<TD><INPUT type="checkbox" name="kiran" value="10611583">TD>
<TD><INPUT type="checkbox" name="kumar" value="10611584"></TD>
<TD><INPUT type="checkbox" name="jags" value="10611585"></TD>



i want to capture (in a servlet) all the values of the checkboxes that are checked and store them in a variable ...(in an array) Any help would be greatly appreciated.
LVL 10
Jagadeesh MBig Data and Splunk ArchitectAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Give them all the same name then

String[] vals = request.getParameterValues("theName");
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hoomanvCommented:
and if you dont want to give the same name to all

Enumeration<String> e = request.getParameterNames();
ArrayList<String> values = new ArrayList<String>();
while(e.hasMoreElements()) {
      values.add(e.nextElement());
}
0
hoomanvCommented:
just correct it
values.add(request.getParameter(e.nextElement()));
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Mick BarryJava DeveloperCommented:
And then if you want them in an array take the code posted by hoomanv and do:

String[] array = values.toArray(new String[0]);
0
hoomanvCommented:
yes thats true but i asked myself what is the usefulness of getting just name's values, whitout the names, they are just pure numbers that are passed to the servlet, so why did he give them names at the first place. however ... its up to him
0
Jagadeesh MBig Data and Splunk ArchitectAuthor Commented:
hoomanv and CEHJ,

I believe your codes would get me only the values of the checked checkboxes....please confirm.
thankz.
0
CEHJCommented:
request.getParameterNames() returns the names of ALL form elements (i.e. including any text areas, buttons etc.), which has been used in the other code to get ALL the form elements
0
Mick BarryJava DeveloperCommented:
you should be prexixing your parameter names so you can identify which are checkboxes

<TD><INPUT type="checkbox" name="names_kiran" value="10611583">TD>
<TD><INPUT type="checkbox" name="names_kumar" value="10611584"></TD>
<TD><INPUT type="checkbox" name="names_jags" value="10611585"></TD>

Enumeration<String> e = request.getParameterNames();
ArrayList<String> values = new ArrayList<String>();
while(e.hasMoreElements()) {
    String name = e.nextElement();
    if (name.startsWith("name_")) {
       values.add(e.nextElement());
    }
}
String[] names = values.toArray(new String[0]);
0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.