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

# 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>