[nobr]Supposing you have something like
<form name="myform" action="/someServletName" method="POST">
<input type="radio" name="radio1" value="A">A<br>
<input type="radio" name="radio1" value="B">B<br>
<input type="radio" name="radio1" value="C">C<br>
<input type="submit">
</form>
when you click the submit button the form parameters (the radiogroup here) will be sent to the servlet. Inside the servlet just do:
String var = request.getParameter("radio1");
And var will contain the value the user selected.[/nobr]
Using this var string how do i create the condition in the if loop ie i have two radio buttons "Search by name" and "search by number".I should solve a query for search by name and another query for search by number.
How do i give the if condition based on the radio button option selected?
The radio button would only send one value to the servlet (the selected one)
In the code I wrote, if the user selects A, var would be "A" So the if would be
if(var.equals("A")){
...
}
Remember to give both radio buttons the same name so there can only be one selected.
Message was edited by:
benubach
Nuh! remeber he has method="POST" (should write post in
lower case , its then xml/xhtml safe habit) and a "post" request
cannot be picked up as param except back in the servlet (or
through the JSF servlet). You must use the servlet interface
and ServletContext to get the param enumeration set returned.
Look in the API docs for the appropriate methods. With a "get"
request the form will be put into the address bar URL-encoded
( note: classes URLencode and URLunencode ) on submit.
With a "post" form the data is only accessible by using javascript
prior to submit() function and not with a submit button type.
e.g.
EL:${param.parname}
JSP scriptlet:String pnme = request.getParameter("parname");
$param["parname"]
e.g. inside a servlet and its service() method:
String pnme = httPrequest.getParameter("parname");
finally in jsp its useful to use a "bean" package and its
syntax system that way you can use a "scope" to make the
data memory/record last on or disappear instanly p/request
from the client (pesistence).
Now... you lost me (and maybe the point)
I was actually catching the params inside a servlet. I don't see why you say request parameters are only available on a servlet when using POST (a jsp is a servlet, remember?) and to be honest, i didn't quite understand what you just said there. It seems you are saying that by using POST the form's parameters are not sent. But they are. Not in the URL as in a GET response (we both agree there) but on the body of the request.
Could you please explain the problem you're trying to point out there?