if with radio

[nobr]Hi,

I am making an "if" statement with radio buttons.

but I don't know what to compare with.

i declared like this:

<form action="2.jsp" method="post" >

<input type="radio" name="ch" value="circle"> Circle

<br>

<input type="radio" name="ch" value="cylinder"> Cylinder

</form>

so, in the 2.jsp i make the comparation for the "circle" like this:

<%

if (request.getParameter("circle") !=null)

{ %>

but when i run it, nothings happens,

is this the right way to do it?

thanks for any response...[/nobr]

[957 byte] By [deroka] at [2007-11-27 5:09:33]
# 1

You should retrieve the parameters by it's name, not by it's value. If there are multiple names (radio groups or checkbox groups), then rather use getParameterValues() which returns a String[]. Also see the [url=http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletRequest.html]ServletRequest API[/url] at the getParameter() and getParameterValues() methods.

BalusCa at 2007-7-12 10:29:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

so, it would be like this?

<%

if (request.getParameterValues("ch").equals("circle"))

{ %>

it does not work either.

what am i doing wrong?

(another thing I forgot to say is that the radio buttons are in a .html and the "if" statement is in the 2.jsp)

deroka at 2007-7-12 10:29:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
> what am i doing wrong?Overlooking the return type of getParameterValues().Ah well, a radio button allows single selection only. getParameter() should indeed be sufficient. Retrieve it by the name instead of value anyway.
BalusCa at 2007-7-12 10:29:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Try request.getParameter() rather than request.getParameterValues().

Your code should also allow for the parameter not being present. Currently your code would throw a null pointer exception if there was no parameter "ch" present.

In scriptlet code, I would do it like this:

<%

String chVal = request.getParameter("ch");

if ("circle".equals(chVal)){

%>

However it comes out much nicer using JSTL/EL than messy scriptlet code.

<c:if test="${param.ch == 'circle'}">

You selected CIRCLE!

</c:if>

evnafetsa at 2007-7-12 10:29:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
thanks a lot guys,evnafets even tough i don't know JSTL/EL,that code looks really interesting...
deroka at 2007-7-12 10:29:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...