Passing Checkbox values from one jsp to another

1.jsp:

 

<%@ 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>

 

2.jsp:

 

<%@ 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>

 

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.

546 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *