JSP radio buttons

I have created 2 radio buttons in my JSP page.When i check one option , how do I take its value to the servlet program and perform corresponding query?Hope my problem is clear.
[190 byte] By [Naveen_Krishnana] at [2007-11-26 21:24:06]
# 1

[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]

benubacha at 2007-7-10 3:04:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Thx. i will try.
Naveen_Krishnana at 2007-7-10 3:04:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Welcome :-) Post again if you get problems
benubacha at 2007-7-10 3:04:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
raj2007a at 2007-7-10 3:04:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

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?

Naveen_Krishnana at 2007-7-10 3:04:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

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

benubacha at 2007-7-10 3:04:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Thanks its working fine.
Naveen_Krishnana at 2007-7-10 3:04:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Welcome :-) Good luck!
benubacha at 2007-7-10 3:04:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

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).

nicephotoga at 2007-7-10 3:04:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
cool...ll try out...
Naveen_Krishnana at 2007-7-10 3:04:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

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?

benubacha at 2007-7-10 3:04:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...