File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Cannot pass checkbox array to Java bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Cannot pass checkbox array to Java bean" Watch "Cannot pass checkbox array to Java bean" New topic

Cannot pass checkbox array to Java bean

Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
Good afternoon,

I am attempting to pass a checkbox array from a jsp page to a java bean with no luck. I am setting properties in the bean using the name property of the input tag. I realize this would be easier with Struts 2 or a similar framework but I am under serious time constraints with no time to spare learning a new framework.

It may be that the checkbox does not conform to an array type of Object or that I'm naming the input tag improperly. The initViewDVO() method is called from an earlier jsp page. Would someone be so kind as to peruse the following code snippets and, perhaps, provide me with a tip or two? Thank you.

Here is my bean:

The array is initialized here:

Here is the relevant jsp:

John Gregory
Ranch Hand

Joined: Oct 05, 2006
Posts: 118

I know that if a group of check boxes is returned in a String []. When I use them in
a form, in my server/jsp code I do something like:

Then to iterate through:

Hope this helps.

Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53

Thank you for your reply. I changed my check box array to a type of String[] rather than Object[] however I still don't get the result I want. I am testing my result by displaying the first element of the array on my web page like so:

The initial value is displayed correctly but does not update when the form is submitted. I have also tried getting the check box using the request parameter you mentioned:

This results in a null pointer exception at the out.print... line.

Thanks again for taking the time to help. I will post when I figure this one out. I'm sure I am missing something simple.
Sunil Vasudevan
Ranch Hand

Joined: Mar 05, 2007
Posts: 107
Jerry Goldsmith wrote:

Jerry Goldsmith wrote:

In your JSP, you are having a different name for your checkbox and in getParameterValue() you are using a different name.

Sunil.V<br />SCJP2, SCWCD1.4, SCBCD1.3
Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
Thanks John. Thanks Sunil.

Actually, the array name in the input tag and the request statement did match. That was just a typo.

I found the real issue. A checkbox array will return null, if nothing is checked, in which case you need to catch the null Pointer Exception. If it is checked, the array element will contain the value from the corresponding input tag. In my case that would be my iterator "i".

Thanks again.

I agree. Here's the link:
subject: Cannot pass checkbox array to Java bean
jQuery in Action, 3rd edition