How can I get/set checkbox value using jstl and delete only those record from the database where the checkbox is checked? can you also advise how to use ternary operators in jstl for this scenario?
SearchStudent.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Lookup Students</title>
</head>
<form method="post" action="deleteStudentServlet" class="form">
<body class="body">
<!-- List results -->
<c:if test="${not empty studentList}">
<table border="1" cellspacing="0" cellpadding="0" :>
<tr>
<th></th>
<th>ID</th>
<th>Title</th>
<th>First Name</th>
<th>Last Name</th>
<th></th>
</tr>
<c:forEach var="students" items="${studentList}">
<tr>
<td><input type="checkbox" name="chkBox"> </td>
<td>${students.studentID}</td>
<td>${students.title}</td>
<td>${students.firstName}</td>
<td>${students.lastName}</td>
<td><c:url value="UDS" var="url">
<c:param name="StudentID" value="${students.studentID}" />
</c:url> <a href="${url}">Edit</a></td>
</tr>
</c:forEach>
</table>
</c:if>
<td><input type="submit" name="submit" value="Delete" ></td>
</form>
<p>There are ${fn:length(studentList)} results.</p>
</body>
</html>
thanks.
The jsp checkbox used in the web pages it allows the user to select more than one values in the options list when we are using multiple checkboxes it may be varied upon the values and also meanwhile it will print the user-defined values as the message that will be specified with the view page after submitting the html ...
Using checkbox user can select more than one options in a form. We need to use html tag <input type="checkbox"> to create the checkboxes in JSP.
The <input type="checkbox"> defines a checkbox. The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices. Tip: Always add the <label> tag for best accessibility practices!
Your checkbox has currently no value associated with the parameter name at all:
<input type="checkbox" name="chkBox">
So it's hard to find out the checked ones. You need to give the checkbox a value which uniquely identifies the selected item. In your particular example, the student ID seems to be an obvious choice:
<input type="checkbox" name="selected" value="${student.studentID}">
(by the way, why are you duplicating the entity name in the property name? why not just name it id
so that you can just self-documentary use ${student.id}
? also your var="students"
is kind of odd, it is referring only one student, so just name it var="student"
; the ${studentList}
can better be named ${students}
)
When the form is submitted, all checked value are available as follows:
String[] selectedStudentIds = request.getParameterValues("selected");
Finally, just pass it through to your DAO/service class which does the business job:
studentService.delete(selectedStudentIds);
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