You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Magnus Jansson <Ad...@vaddo.fhsk.se> on 2001/09/17 07:32:38 UTC

Get value from a checkbox

Hi! I'm a newbi on jsp, and having trouble with forms.

I have a form on one page and want to send the information to a jsp page. My form includes a couple of checkboxes and I only gets runtime errors when I try to get the values from the checkbox.

Fact is I dont have a clue how to get the value from a checkbox in jsp with the request.getParameter.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Magnus Jansson
IT-Manager
Väddö folkhögskola
760 40 Väddö
Sweden

Phone: +46 (0) 176-528 00
Cellular: +46 (0) 70-370 33 16
Fax: +46 (0) 176-528 28
http://www.vaddo.fhsk.se (work)
http://www.jason.pp.se (private)
ICQ: 52797837


Re: Get value from a checkbox

Posted by Mark Himsley <ma...@mdsh.com>.
On Mon, 17 Sep 2001 07:32:38 +0200 you wrote:

>Hi! I'm a newbi on jsp, and having trouble with forms.
>
>I have a form on one page and want to send the information to a jsp page. My form includes a couple of checkboxes and I only gets runtime errors when I try to get the values from the checkbox.
>
>Fact is I dont have a clue how to get the value from a checkbox in jsp with the request.getParameter.

This is very basic. I suggest you get a book on HTML, HTTP and
JSP/Servlets, but lets see if I can help with this specific problem.

If an HTML form is submitted with an unchecked check box then that
parameter is not even returned to the server. Therefor you have to check
that the parameters exists or not:

if (request.getParameter("checkbox") != null) { /* checkbox checked */ }

What I often do is:

1) create the checkboxes with a value of 1

<input type="checkbox" name="checkbox" value="1">

2) write a bean to accept all the request parameters. If you declare the
   instance variables initially equalling false:

boolean checkbox = false;

   and create a setter method which sets the value of this instance
   variable

public void setCheckbox(int value) {
        checkbox = true;
}

3) all you need to so then is 

<jsp:useBean id="form" scope="request" class="your.bean"/>
<jsp:setProperty name="form" property="*"/>

4) now all your form request parameters are stored in the bean and you
also do not need to do any checking for null's because you created default
values for all of your request parameters.

--
Mark Himsley
In Acton