Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in reading from checkbox

 
Florence Cheung
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Raja Shekhar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic