skyknight
10-31-2005, 01:28 PM
who can tell me how to use servlet to get values from check box? Thank you.
|
Click to See Complete Forum and Search --> : question about get value from checkbox skyknight 10-31-2005, 01:28 PM who can tell me how to use servlet to get values from check box? Thank you. BigDog 10-31-2005, 02:02 PM It would be like retriving any other value, except that since a checkbox can have multiple values, you need to use the ServletRequest's object getParameterValues() method. This will return an array of strings of all the values that were selected in the checkboxes. skyknight 11-01-2005, 01:43 PM I was used the getParameterValues() method, however if the check box did not have any value submit to servlet, then still have error. how can I solve this problem? BigDog 11-01-2005, 04:19 PM You need to check to see if the value is null. If there was nothing checked, the array produced would have no values. So when you read the value from the array, make sure you also check to see if it is null. If it is null, then no values were entered. I'm pretty sure that you will get an arry object returned back, but the first element will contain null, if nothin was selected for the check box. skyknight 11-02-2005, 12:27 AM Should I check in javascript ? because the following statement have error when no value submit to servlet from check box: String testing[] = request.getParameterValue("checkbox1"); if not, how can I check in servlet? Thank you for your help ^^ BigDog 11-02-2005, 07:39 AM Make sure you are using getParameterValues() , not getParameterValue(). You would do the following: String testing[] = request.getParameterValues("checkbox1"); if(testing == null) { out.println("Nothing selected!"); } else { //do something with the data } If you want to check that someone has selected a checkbox BEFORE they submit the form, then you would use Javascript. The above code would instead be used in the servlet to check they values after they have submitted the form. skyknight 11-02-2005, 08:00 AM Thank you for your help, BigDog. webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |