Simplest way: just let the link point to a JSP page and pass row ID as parameter:
<a href="delete.jsp?id=1">delete</a>
And in delete.jsp
(I'm leaving obvious request parameter checking/validating aside):
<% dao.delete(Long.valueOf(request.getParameter("id"))); %>
This is however a pretty poor practice (that was still an understatement) and due to two reasons:
HTTP requests which modifies the data on the server side should not be done by GET, but by POST. Links are implicit GET. You should use a <form method="post">
and a <button type="submit">
. You can however use CSS to style the button to look like a link.
Putting business logic (functions as you call it) in a JSP using scriptlets (those <% %>
things) is discouraged. You should use a Servlet to control, preprocess and postprocess HTTP requests.
Since you didn't tell any word about a servlet in your question, I suspect that you're already using scriptlets to load data from DB and display it in a table. That should also be done by a servlet.
Here's a basic kickoff example how to do it all. I have no idea what the table data represents, so let take Product
as an example.
public class Product {
private Long id;
private String name;
private String description;
private BigDecimal price;
// Add/generate public getters and setters.
}
And then the JSP file which uses JSTL (just drop jstl-1.2.jar in /WEB-INF/lib
to install it) to display the products in a table with a delete button in each row:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<form action="products" method="post">
<table>
<c:forEach items="${products}" var="product">
<tr>
<td>${product.name}</td>
<td>${product.description}</td>
<td>${product.price}</td>
<td><button type="submit" name="delete" value="${product.id}">delete</button></td>
</tr>
</c:forEach>
</table>
</form>
Name it products.jsp
and put it in /WEB-INF
folder so that it's not directly accessible by URL (so that the enduser is forced to call the servlet for that).
Here's how the servlet roughly look like:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// This method is invoked during every GET request (link, bookmark, redirect)
List<Product> products = productDAO.list();
request.setAttribute("products", products); // Will be available as ${products} in JSP.
request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// This method is invoked during every POST request (form submit).
String delete = request.getParameter("delete");
if (delete != null) { // Is the delete button pressed?
productDAO.delete(Long.valueOf(delete));
}
response.sendRedirect("products"); // Go back to page with table.
}
Name it com.example.ProductServlet
(which extends HttpServlet
) and map this servlet on an url-pattern
of /products
in web.xml
like follows:
<servlet>
<servlet-name>productServlet</servlet-name>
<servlet-class>com.example.ProductServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>productServlet</servlet-name>
<url-pattern>/products</url-pattern>
</servlet-mapping>
Deploy and run it. You can open the table by http://example.com/contextname/products.
See also: