Servlet question - how to collect checked items of a checkbox list ?

D

Daku

Could some Java guru please help ? I have a standard Servlet class, in
the 'doPost' method of
which I generate a Web page, which dynamically
generates a list of checkboxes while displaying the results of a
database query. The form in the dynamically generated Web page simply
does a HTTP 'POST' to the same servlet. So, if I select a set of items
in the check box list, and then do a POST, how do I recover the
checked items ?
Any hints, suggestions would be of immense help. Thanks in advance.
 
M

markspace

Daku said:
Could some Java guru please help ? I have a standard Servlet class, in
the 'doPost' method of
which I generate a Web page, which dynamically
generates a list of checkboxes while displaying the results of a
database query. The form in the dynamically generated Web page simply
does a HTTP 'POST' to the same servlet. So, if I select a set of items
in the check box list, and then do a POST, how do I recover the
checked items ?
Any hints, suggestions would be of immense help. Thanks in advance.


String[] checkBoxes = request.getParamterValues("name");
for( String s : checkBoxes ) {
out.println( "<br />checkBox: "+s );
}


You might want to look into EL and JSP too. Here's an online free class
for JEE:

<http://www.javapassion.com/j2ee/>
 
W

Wojtek

markspace wrote :
Daku said:
Could some Java guru please help ? I have a standard Servlet class, in
the 'doPost' method of
which I generate a Web page, which dynamically
generates a list of checkboxes while displaying the results of a
database query. The form in the dynamically generated Web page simply
does a HTTP 'POST' to the same servlet. So, if I select a set of items
in the check box list, and then do a POST, how do I recover the
checked items ?
Any hints, suggestions would be of immense help. Thanks in advance.


String[] checkBoxes = request.getParamterValues("name");
for( String s : checkBoxes ) {
out.println( "<br />checkBox: "+s );
}

The problem with this is the checkboxes which are not checked, as the
specification allows for a non-checkbox to never be returned.

[*] One
[ ] Two
[*] Three

If you use the loop above you will see that 2 checkboxes have been
checked, since the browser returns 1 and 3, but you will not know WHICH
two.

Rather than using <.. name="name">, dynamically generate the name:

for ( int index = 0; index < maxIndex; index++)
buffer.append("<... name=\"name" + index + "\">);

and then store the index count in a hidden field:

buffer.append("<input type=\"hidden\" name=\"max\" value=\"" + maxIndex
+ "\">");

In the response first read the "max" parameter, then loop that many
times looking for "name" + index. Any retrieval which is null means a
checkbox which was not checked.

Um, this is rough code, probably will cause syntax errors and is merely
meant as a guide.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top