File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes  problem in reading from checkbox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark " problem in reading from checkbox" Watch " problem in reading from checkbox" New topic
Author

problem in reading from checkbox

Florence Cheung
Greenhorn

Joined: Mar 18, 2001
Posts: 24
Hi,
I have a jsp file which is called multiple times by some servlets.This jsp file uses a tag. In the tag, I display a couple of checkboxes.At the end of the jsp file, I forward the action to a servlet which will check the result of the checkboxes.
--- jsp file:
//this jsp file is called by some servlets.
if(some condition matches)
<tagPrefix:tagName />
<form action=theCheckServletURL method=post>
<input type=submit value="buttonName">
</form>

--- in tag:
//some codes here
out.println("<table>");
for(//the loop condition){
//dynamically get checkboxName here.
out.println("<tr>");
out.println("<td>");
out.println("<input type=checkbox name="+checkBoxName+"> );
out.println("</td>");
out.println("</tr>");
}
out.println("</table"> );
--- in checkServlet:
//get checkBoxName here
String checkBoxResult=request.getParameter(checkBoxName);
//run some other logic.Then forward to the jsp file to
//display a different group of checkboxes.

I checked the checkBoxName in checkServer & tag file,they are the same name. But in the checkServlet, the checkBoxResult is always "null".What am I missing here? Please help!
Thanks!
Florence

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
The first thing I would do is use view source to see exactly what the JSP wrote to the browser. I suspect the line:
out.println("<input type=checkbox name="+checkBoxName+"> );
is not creating what you want, you need to have quote marks around the attribute for name. ie:
type=checkbox name="somename"
Personally I always put quotes around all attributes in spite of the fact that some browsers don't care.
Bill
Florence Cheung
Greenhorn

Joined: Mar 18, 2001
Posts: 24
Hey,Bill,
I checked the tag side & server side, the names of the checkbox are the same. I tried single quotes & double quotes ,and also viewed the source code,looks the browser interpreted correctly.But I still got "null" value on the servlet side.Why is that?
Florence
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
I know a checkbox's value/name is only sent to the servlet if the checkbox is checked. if it's not checked, nothing is sent. Hence it will be null.

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Raja Shekhar
Ranch Hand

Joined: Apr 02, 2001
Posts: 49
hi
out.println("<input type=checkbox name="+checkBoxName+"> );

Just check it could be like this...
out.println("<input type=checkbox name=<%=checkBoxName%>");
the varibale could a JSP varaible
Raja Shekhar B

Raja Shekhar
Ranch Hand

Joined: Apr 02, 2001
Posts: 49
hi
out.println("<input type=checkbox name="+checkBoxName+"> );

Just check it could be like this...
out.println("<input type=checkbox name=<%=checkBoxName%>");
the varibale could a JSP varaible
Raja Shekhar B

Raja Shekhar
Ranch Hand

Joined: Apr 02, 2001
Posts: 49
hi
out.println("<input type=checkbox name="+checkBoxName+"> );

Just check it could be like this...
out.println("<input type=checkbox name=<%=checkBoxName%>");
the varibale could a JSP varaible
Raja Shekhar B

Smita Tyagi
Greenhorn

Joined: May 09, 2001
Posts: 21
Hello Flornce,
You are going to get null values until you assign values to your checkboxes as
someval = request.checkboxvalue;
out.println("<input type=checkbox name="+checkBoxName+" value = "+someval+">" );
try this it must work.
Smita
Yogesh Kapoor
Greenhorn

Joined: May 01, 2001
Posts: 6
The checkbox values become a part of the request only when the checkbox is checked(set). Assigning a value to the checkbox does not matter. If the checkbox is not checked the request.getParameter() will always return null as there would be no parameter in the request with the name of your checkbox.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: problem in reading from checkbox
 
Similar Threads
How to get Names of selected checkboxes?
need help :: multiple rows update without primary key
save checked value in checkbox to database
Trying to display checkbox checked values in new jsp page
embedded js file in doGet