Greetings of the day !!!
I have created a jsp file as follow :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org /TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="TestAms" method="post">
<%
for(int i=0; i<5; i++){
%>
<input type="radio" name="<%= i%>" value="<%=i %>">
<%
}
%>
<input type="submit" value="SUBMIT">
</form>
</body>
</html>
This program creates 5 radio buttons in a loop having name and value same as the loop variable.Now I want to get the values of the radio buttons in the servlet. The code for my servlet is as follow :
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestAms extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
for (int i=0; i<5; i++){
String s[] = new String[5];
s[i] = request.getParameter("<%= i%>");
out.println(s[i]);
}
}
}
But I am ending with only 5 "null" values. I know where the problem is. But I am unable to find a way to pass the loop variables to request.getParameter() method. Please help me ..... Thnx in advance !
In java, you should write:
s[i] = request.getParameter(Integer.toString(i));
<%= ... %> are JSP scriptlet, not for Java class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With