[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 458
  • Last Modified:

WHEN PAGE SUBMITS how to retrieve checked value in servlet;;selected radio button value should come in servlet

for(int i=0;i<list.size();i++)
{
<td><input type="radio" id="empCheck<%=i%>" name="empCheck" onClick="selectEmp('<%=empHeader.empId%>',<%=i%>)"></td>
<td><input type=hidden id='empId<%=i%>' name="empId" value='<%= empHeader.empId%>'> </td>
}
function selectEmp(refId,i)
{
     
     if(document.getElementById('empCheck'+i).checked)
          document.getElementById('empId'+i).value=refId;
     else
          document.getElementById('empId'+i).value="";    
     var len=document.forms[0].empId.length;

   for(var i=0;i<len;i++)
   {
    if(document.forms[0].empId[i].checked  == true)  
    document.getElementById('empId'+i).value=refId;
   }

   
}

function ex()
                        {
                        Url      =      'empservlet';
                              document.forms[0].action = Url;
                        
                        document.forms[0].submit();
                        }

<input type="button" name="b1" VALUE="submit" class = 'input' onClick="ex()">

                        
WHEN PAGE SUBMITS how to retrieve checked value in servlet;;selected radio button value should come in servlet.

if i use request.getParameter("empId") then " " is coming and if i use request.getParameterValues("empId") all values r coming instead of checked one.



see this another sample jsp;

second_page.jsp

from the DB iterate the loop and display the results.presently i hardcoded the values.

when user clicked particular radio button its empId and corresponding empseqno values should come ..

<script>

function ex()
{
                        Url      =      'second_page.jsp';
                        document.forms[0].action = Url;
                        document.forms[0].submit();
}
</script>

<form action="second_page.jsp" method="post">
<table border="0" width="820" cellpadding="2" cellspacing="1" >
<tr class='formheader' align=center>
<td>
<input type="radio" id="empId" name="empId" value="11111">111111
<input type="hidden" id="empseqno" name="empseqno" value="aaaa">
</td>
</tr>
<tr class='formheader' align=center>
<td>
<input type="radio" id="empId" name="empId" value="222222">22222
<input type="hidden" id="empseqno" name="empseqno" value="bbbb">
</td>
</tr>

</table>
<input type="button" value="Submit" onclick="ex()">

</form>


At second_page.jsp, you do this to retrieve and display:

<%
out.println(request.getParameter("empId"));
out.println(request.getParameter("empseqno"));

%>


0
chaitu chaitu
Asked:
chaitu chaitu
  • 4
  • 3
  • 2
1 Solution
 
matthew016Commented:
Use attribute value in your html :

<input value="M" name="gender" type="radio">Male
<input value="F" name="gender" type="radio">Female

And in your servlet the usual thing :

String sexe = request.getParameter("gender");

U will retrieve the value of attribute value

0
 
chaitu chaituAuthor Commented:
see this another sample jsp;



from the DB iterate the loop and display the results.presently i hardcoded the values.

when user clicked particular radio button its empId and corresponding empseqno values should come ..

second_page.jsp
*************
<script>

function ex()
{
                    Url     =     'second_page.jsp';
                    document.forms[0].action = Url;
                    document.forms[0].submit();
}
</script>

<form action="second_page.jsp" method="post">
<table border="0" width="820" cellpadding="2" cellspacing="1" >
<tr class='formheader' align=center>
<td>
<input type="radio" id="empId" name="empId" value="11111">111111
<input type="hidden" id="empseqno" name="empseqno" value="aaaa">
</td>
</tr>
<tr class='formheader' align=center>
<td>
<input type="radio" id="empId" name="empId" value="222222">22222
<input type="hidden" id="empseqno" name="empseqno" value="bbbb">
</td>
</tr>

</table>
<input type="button" value="Submit" onclick="ex()">

</form>


At second_page.jsp, you do this to retrieve and display:

<%
out.println(request.getParameter("empId"));
out.println(request.getParameter("empseqno"));

%>
0
 
matthew016Commented:
the value of empId is ok now ?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
chaitu chaituAuthor Commented:
its ok but corresponding empseqno how will i get??
0
 
KuldeepchaturvediCommented:
you will have to change the names of hidden fields as well, otherwise you will keep on getting all the values..

<form action="second_page.jsp" method="post">
<table border="0" width="820" cellpadding="2" cellspacing="1" >
<tr class='formheader' align=center>
<td>
<input type="radio" id="empId1" name="empId1" value="11111">111111
<input type="hidden" id="empseqno1" name="empseqno1" value="aaaa">
</td>
</tr>
<tr class='formheader' align=center>
<td>
<input type="radio" id="empId2" name="empId2" value="222222">22222
<input type="hidden" id="empseqno2" name="empseqno2" value="bbbb">
</td>
</tr>

in your servlet
if(request.getParemeter("empId1")!=null)
request.getParameter("empseqno2");
you can get all the parameter names in a enumeration and iterate it thru to make it generic
0
 
chaitu chaituAuthor Commented:
how do i know that which radio button value is clicked??

execute the below sample jsp;

this is a sample jsp ;when i select one radio button and enter submit how to capture the empNO to the corresponding empId;

<script>

function ex()
{
                    Url     =     'second_page.jsp';
                    document.forms[0].action = Url;
                    document.forms[0].submit();
}
</script>

<form action="second_page.jsp" method="post">
<table border="0" width="820" cellpadding="2" cellspacing="1" >

<%
for(int i=0;i<10;i++)
{%>
<tr class='formheader' align=center>
<td>
<input type="radio" id="empId<%=i%>" name="empId" value="<%=i%>"><%=i%>
<input type="hidden" id="empNO<%=i%>" name="empNO" value="AAA<%=i%>">
</td>
</tr>
<%}%>

</table>
<input type="button" value="Submit" onclick="ex()">

</form>


At second_page.jsp, you do this to retrieve and display:

<%
out.println(request.getParameter("empId"));
out.println(request.getParameter("empNO"));

%>

0
 
KuldeepchaturvediCommented:
for(int i=0;i<10;i++)
{%>
<tr class='formheader' align=center>
<td>
<input type="radio" id="empId<%=i%>" name="empId<%=i%>" value="<%=i%>"><%=i%>
<input type="hidden" id="empNO<%=i%>" name="empNO<%=i%>" value="AAA<%=i%>">
</td>
</tr>
<%}%>

</table>
<input type="button" value="Submit" onclick="ex()">

</form>


At second_page.jsp, you do this to retrieve and display:

<%
for(int i=0;i<10;i++)
{
if(request.getParameter("empId"+i)!=null)
{
out.println(request.getParameter("empId"+i));
out.println(request.getParameter("empNO"+i));
}
}

%>
0
 
chaitu chaituAuthor Commented:
with the above code i will get all empid's not exactly checked one
0
 
KuldeepchaturvediCommented:
unless the radio button is checked, the value is not sent to the server... so rest all you empid values will be null... thats why I have placed that if condition in the loop
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now