Passing Checkbox values from one jsp to another

1.jsp:

 

[html]<%@ 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 method="post" action="2.jsp">
My Favourite Colors are
<input type="checkbox" name="cb1" value="blue">Blue
<input type="checkbox" name="cb1" value="green">Green
<input type="checkbox" name="cb1" value="Yellow">Yellow
<input type="checkbox" name="cb1" value="Red">Red
<input type="checkbox" name="cb1" value="white">White
<input type="submit">
</form>
</body>
</html>[/html]

 

2.jsp:

 

[html]<%@ 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>
<%String[] getting = request.getParameterValues("cb1");
for(int i=0;i<getting.length;i++){
out.println(getting[i]);
}
System.out.println(getting);

%>

</body>
</html>[/html]

 

Note: 

getParameterValues to store the more than one values.

If you want to store only one value then you can use getParameter(“cb1”);

cb1 is the name of the checkbox which you have mentioned in the jsp 1.

Leave a Reply