Solved

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

Posted on 2006-10-30
9
436 Views
Last Modified: 2011-09-20
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
Comment
Question by:chaitu chaitu
  • 4
  • 3
  • 2
9 Comments
 
LVL 9

Expert Comment

by:matthew016
ID: 17838321
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 17839842
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
 
LVL 9

Expert Comment

by:matthew016
ID: 17840986
the value of empId is ok now ?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 20

Author Comment

by:chaitu chaitu
ID: 17841160
its ok but corresponding empseqno how will i get??
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 17844585
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 17847865
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
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 500 total points
ID: 17849224
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 17849292
with the above code i will get all empid's not exactly checked one
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 17849302
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
object methods vs static methods 2 100
Looking for some programs to resell or buy source codes 1 77
Handle form fields in spring mvc controller 1 66
jsp login check 12 43
If you are looking at this article, you have most likely been hit by some version of ransomware and are trying to find out if there is anything you can do, or what way you should react - READ ON!
Giving access to ESXi shell console is always an issue for IT departments to other Teams, or Projects. We need to find a way so that teams can use ESXTOP for their POCs, or tests without giving them the access to ESXi host shell console with a root …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question