It's not a secret anymore!*
The moose likes Servlets and the fly likes dynamic Checkboxe value retrieval Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "dynamic Checkboxe value retrieval" Watch "dynamic Checkboxe value retrieval" New topic
Author

dynamic Checkboxe value retrieval

vineet varghese
Greenhorn

Joined: Aug 16, 2011
Posts: 13
hi all.. i am dynamically populating my jsp page table from database values with checkboxes in each row..i am not able to retrieve the checkbox value into my servlet even after using getparametervalues
its just not excuting..any solution ?
the value of the checkbox is dynamic data retrieved from the DB in each loop ...with same name ..help please
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

So, just to clarify:

You have some process which gets data from a database and, from that data, produces some HTML with checkboxes in it. You call that process "populating my JSP page table".

And in this HTML you have some link which is supposed to transmit the values of those checkboxes to a servlet.

Do I have this correct? If so then you have two separate steps; you should be able to examine them separately and determine which of the two steps is not working the way you want it to. This would be a better way to handle the problem than simply considering the whole thing as a big ball of mud.
vineet varghese
Greenhorn

Joined: Aug 16, 2011
Posts: 13
thank you replying to my first post paul..
yes you got it right. the first part is working fine.i have all the DB details in my jsp page right now.
i just need to get all the "Checked box" values into my servlet on clicking submit button.this is where i am facing problem
any sample code will be helpful thank you.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You want sample HTML code for sending checkbox data to a servlet? It would be more practical to look at your HTML code. So how about if we start from that?
vineet varghese
Greenhorn

Joined: Aug 16, 2011
Posts: 13
heres my jsp code....purchaseis my servlet where i want the checked box values...it may not be as efficient as i am new to this..thank you



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>

<%@page
import="com.online.vineetkirana.persistence.productsDAO"%>
<%@page
import ="com.online.vineetkirana.persistence.productsHibernateDAO"%>
<%@page
import="com.online.vineetkirana.vo.products"%>




<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Greefies Css Template</title>
<link rel="stylesheet" type="text/css" href="style/style.css" media="screen" />

</head>
<body>

<div id="main_container">
<!-- Common Include : start -->
<div id="header">
<div id="logo"><a href="home.html"><img src="images/logo.jpg" alt="" title="" border="0" /></a></div>
<jsp:include page="portal/common/menu.jsp"></jsp:include>
</div>
<jsp:include page="portal/common/header.jsp"></jsp:include>
<div id="main_content">
<jsp:include page="portal/common/productftSideBar.jsp"></jsp:include>
<!-- Common Include : end -->
<div id="right_content">
<!-- **********Main Body : Start*********** -->
<h2right>Welcome</h2right>

<div class="products_box">
<!-- Form : start -->
<!-- First part of : start -->
<p>

<form action="purchase" method="post" name="frm">
<input type="submit" size="30" name="sub" value="Create" />
<%
products us=new products();
productsDAO productdao =new productsHibernateDAO();

List <products> ufo= productdao.getAll_products();



out.println("<table border=1 style=\"margin-left: 82px;\">");
out.print("<th><b>ID </th>");
out.print("<th><b>NAME </th>");
out.print("<th><b>CATEGORY </th>");
out.print("<th><b>PRICE </th>");



for(int i=0;i<ufo.size();i++)
{


out.println("<tr><td><b>" + ufo.get(i).getProductid()+ "</td><td><b>" + ufo.get(i).getName() +"</td><td><b>" + ufo.get(i).getType()+"</td><td><b>"+ufo.get(i).getRate());
out.println("<td><b><input type=\"checkbox\" name=\"productname\" style=\"margin-left: 22px;\"value=ufo.get(i).getName()<b></b>/</td>");
out.println("<td><b><input type=\"checkbox\" name=\"productname\" style=\"margin-left: 22px;\"value=24<b></b></td>");

}

out.print("</table>");

out.println("");
out.println("");
out.println("");
%>

</FORM>

</p>
<!-- First part of : End -->
</div>

</p>

</div>
</div><!--end of right content-->
<!-- **********Main Body : End*********** -->


<div style=" clear:both;"></div>
</div><!--end of main content-->


<jsp:include page="portal/common/footer.jsp"></jsp:include>



</div> <!--end of main container-->
</body>
</html>
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

No, it's extremely inefficient for somebody to go through that and try to guess what HTML it would generate. That's why I asked you to post the HTML code. Use the "display page source" feature of your browser to get it.
vineet varghese
Greenhorn

Joined: Aug 16, 2011
Posts: 13
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Greefies Css Template</title>
<link rel="stylesheet" type="text/css" href="style/style.css" media="screen" />

<script type="text/javascript">
function show_alert()
{
alert("Created sucessfuly ");
}
</script>
</head>
<body>





<div id="main_container">
<!-- Common Include : start -->
<div id="header">
<div id="logo"><a href="home.html"><img src="images/logo.jpg" alt="" title="" border="0" /></a></div>

<div id="menu">
<ul>
<li><a href="home.jsp" title="">Home</a></li>

<li><a href="shopping.jsp" title="">Shop Now</a></li>
<li><a href="#" title="">Order Status</a></li>
<li><a href="#" title="">Contact Us</a></li>
<li><a href="customerdetails.jsp" title=""></a></li>


</ul>
</div>
</div>
<div class="green_box">
<div class="clock">

<!-- <img src="images/clock.png" alt="" title="" /> -->
</div>
<div class="text_content">
<h1> GreenZone KiranaS </h1>
<p class="green">

</p>
</div>

<!-- <div id="right_nav">
<ul>
<li style="margin-left: 186px;">
<a title="" href="home.html"> Heloo </a>
</li>
<li style="margin-left: 186px;">
<a title="" href="home.html"></a>
</li>
<li style="margin-left: 186px;">
<a title="" href="home.html"></a>
</li>
<li style="margin-left: 186px;">
<a title="" href="home.html"></a>
</li>
<li style="margin-left: 186px;">
<a title="" href="home.html"></a>
</li>
</ul>
</div>
-->

</div><!--end of green box-->
<div id="main_content">

<div id="left_content">
<h2left>Welcome </h2left>
<p>

</p>

<div id="left_nav">
<ul>
<li><a href="productadd.jsp" title="">Add New </a></li>
<li><a href="productdeleter.jsp" title="">Delete</a></li>

<li><a href="productlist.jsp" title="">View All Products</a></li>

</ul>
</div>

<p class="clear">


</p>

</div><!--end of left content-->
<!-- Common Include : end -->
<div id="right_content">
<!-- **********Main Body : Start*********** -->

<h2right>Welcome</h2right>

<div class="products_box">
<!-- Form : start -->
<!-- First part of : start -->
<p>

<form action="purchase" method="post" name="frm">
<input type="submit" size="30" name="sub" value="Create" />
<table border=1 style="margin-left: 82px;">
<th><b>ID </th><th><b>NAME </th><th><b>CATEGORY </th><th><b>PRICE </th><tr><td><b>1</td><td><b>kissan jam</td><td><b>breakfast</td><td><b>89

<td><b><input type="checkbox" name="productname" style="margin-left: 22px;"value=ufo.get(i).getName()<b></b></td>
<tr><td><b>2</td><td><b>honey</td><td><b>breakfast</td><td><b>33
<td><b><input type="checkbox" name="productname" style="margin-left: 22px;"value=ufo.get(i).getName()<b></b></td>
<tr><td><b>3</td><td><b>katchup</td><td><b>breakfast</td><td><b>34
<td><b><input type="checkbox" name="productname" style="margin-left: 22px;"value=ufo.get(i).getName()<b></b></td>
<tr><td><b>4</td><td><b>cornflakes</td><td><b>breakfast</td><td><b>34
<td><b><input type="checkbox" name="productname" style="margin-left: 22px;"value=ufo.get(i).getName()<b></b></td>
<tr><td><b>5</td><td><b>kellogs</td><td><b>breakfast</td><td><b>77

<td><b><input type="checkbox" name="productname" style="margin-left: 22px;"value=ufo.get(i).getName()<b></b></td>
<tr><td><b>6</td><td><b>quaker oats</td><td><b>breakfast</td><td><b>100
<td><b><input type="checkbox" name="productname" style="margin-left: 22px;"value=ufo.get(i).getName()<b></b></td>
<tr><td><b>7</td><td><b>kellogs honey</td><td><b>breakfast</td><td><b>29
<td><b><input type="checkbox" name="productname" style="margin-left: 22px;"value=ufo.get(i).getName()<b></b></td>
<tr><td><b>8</td><td><b>kellogs honey</td><td><b>breakfast</td><td><b>44
<td><b><input type="checkbox" name="productname" style="margin-left: 22px;"value=ufo.get(i).getName()<b></b></td>
<tr><td><b>9</td><td><b>dabur honey</td><td><b>breakfast</td><td><b>60

<td><b><input type="checkbox" name="productname" style="margin-left: 22px;"value=ufo.get(i).getName()<b></b></td>
<tr><td><b>10</td><td><b>rassam powder</td><td><b>cooking</td><td><b>45
<td><b><input type="checkbox" name="productname" style="margin-left: 22px;"value=ufo.get(i).getName()<b></b></td>
<tr><td><b>11</td><td><b>everest garam masala</td><td><b>cooking</td><td><b>66
<td><b><input type="checkbox" name="productname" style="margin-left: 22px;"value=ufo.get(i).getName()<b></b></td>
<tr><td><b>12</td><td><b>chiken masala</td><td><b>cooking</td><td><b>88
<td><b><input type="checkbox" name="productname" style="margin-left: 22px;"value=ufo.get(i).getName()<b></b></td>
<tr><td><b>13</td><td><b>chat masala</td><td><b>cooking</td><td><b>30

<td><b><input type="checkbox" name="productname" style="margin-left: 22px;"value=ufo.get(i).getName()<b></b></td>
<tr><td><b>14</td><td><b>anjeer</td><td><b>Edibles</td><td><b>100
<td><b><input type="checkbox" name="productname" style="margin-left: 22px;"value=ufo.get(i).getName()<b></b></td>
<tr><td><b>15</td><td><b>butter</td><td><b>Breakfast</td><td><b>30
<td><b><input type="checkbox" name="productname" style="margin-left: 22px;"value=ufo.get(i).getName()<b></b></td>
<tr><td><b>16</td><td><b>butter</td><td><b>Breakfast</td><td><b>30
<td><b><input type="checkbox" name="productname" style="margin-left: 22px;"value=ufo.get(i).getName()<b></b></td>
<tr><td><b>17</td><td><b>butter</td><td><b>Breakfast</td><td><b>30

<td><b><input type="checkbox" name="productname" style="margin-left: 22px;"value=ufo.get(i).getName()<b></b></td>
</table>













</FORM>

</p>
<!-- First part of : End -->
</div>

</p>


</div>
</div><!--end of right content-->
<!-- **********Main Body : End*********** -->


<div style=" clear:both;"></div>
</div><!--end of main content-->


<div id="footer">


<ul>
<li><a href="#"></a> </li>

<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href=></a></li>
</ul>

</div>



</div> <!--end of main container-->
</body>
</html>


HOPE this is what you want
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Okay. So when you look in that HTML and try to find a checkbox, this is what you find:

That wasn't what you intended to generate, was it?>
vineet varghese
Greenhorn

Joined: Aug 16, 2011
Posts: 13
i intended the same html.
ufo.get(i).getName() gives me the data from the list that i fetched from the DB
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
vineet varghese wrote:i intended the same html.
ufo.get(i).getName() gives me the data from the list that i fetched from the DB

Since this is the HTML source, if this statement ufo.get(i).getName() gives you a value, it has to be replaced by the value.
vineet varghese
Greenhorn

Joined: Aug 16, 2011
Posts: 13
but i am using a loop to fetch DB value and display the value on the page.
if i insert the value itself it wont be dynamic...right ?
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
you misunderstood me!!!
when you have this
ufo.get(i).getProductid()
in your JSP, what is it displaying in your HTML source?? It is displaying the product id's 1,2,3...

Similarly when you have
ufo.get(i).getName()
in your JSP, when you view the HTML source the Name should be present there, instead you have the same ufo.get(i).getName()
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
replace this
out.println("<td><b><input type=\"checkbox\" name=\"productname\" style=\"margin-left: 22px;\"value=ufo.get(i).getName()<b></b>/</td>");
part of your JSP with this

out.println("<td><b><input type=\"checkbox\" name=\"productname\" style=\"margin-left: 22px;\"value="+ufo.get(i).getName()+"<b></b>/</td>");


I dont know for what purpose you are doing this, but using scriptlets is discouraged in a JSP.
vineet varghese
Greenhorn

Joined: Aug 16, 2011
Posts: 13
hey thank you bhanu chowdary it worked
yes i know its not a good practice but i am not using it in many places ....mostly used servlet
thanks a lot....will get more doubts
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dynamic Checkboxe value retrieval
 
Similar Threads
Unchanged dynamically created check boxes not in request object.
strange checkbox problem
Jquery Wrong Markup
Valid JSP Parameters
Problem with selected checkbox value in struts 1.2