How do I pass current item to Java method by clicking a hyperlink or button in JSP page? -
i have html table rows fetched database displayed in table. want user able delete row clicking on delete hyperlink or button besides each row.
how invoke jsp function on page, when user clicks on each of delete hyperlinks or button, can delete row's entry database? should <a>
or <button>
tag have call jsp function?
note need call jsp function, not javascript function.
simplest way: let link point jsp page , pass row id 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 pretty poor practice (that still understatement) , due 2 reasons:
http requests modifies data on server side should not done get, post. links implicit get. imagine happen when web crawler googlebot tries follow delete links. should use
<form method="post">
,<button type="submit">
delete action. can use css style button link. edit links preload item prefill edit form can safely get.putting business logic (functions call it) in jsp using scriptlets (those
<% %>
things) discouraged. should use servlet control, preprocess , postprocess http requests.
since didn't tell word servlet in question, suspect you're using scriptlets load data db , display in table. should done servlet.
here's basic kickoff example how all. have no idea table data represents, let take product
example.
public class product { private long id; private string name; private string description; private bigdecimal price; // add/generate public getters , setters. }
and jsp file uses jstl (just drop jstl-1.2.jar in /web-inf/lib
install it) display products in table edit link , delete button in each row:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> ... <form action="products" method="post"> <table> <c:foreach items="${products}" var="product"> <tr> <td><c:out value="${fn:escapexml(product.name)}" /></td> <td><c:out value="${product.description}" /></td> <td><fmt:formatnumber value="${product.price}" type="currency" currencycode="usd" /></td> <td><a href="${pagecontext.request.contextpath}/product?edit=${product.id}">edit</a></td> <td><button type="submit" name="delete" value="${product.id}">delete</button></td> </tr> </c:foreach> </table> <a href="${pagecontext.request.contextpath}/product">add</a> </form>
name products.jsp
, put in /web-inf
folder it's not directly accessible url (so enduser forced call servlet that).
here's how servlet (validation omitted brevity):
@webservlet("/products") public class productsservlet extends httpservlet { private productdao productdao; // ejb, plain dao, etc. @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { list<product> products = productdao.list(); request.setattribute("products", products); // available ${products} in jsp. request.getrequestdispatcher("/web-inf/products.jsp").forward(request, response); } @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string delete = request.getparameter("delete"); if (delete != null) { // delete button pressed? productdao.delete(long.valueof(delete)); } response.sendredirect(request.getcontextpath() + "/products"); // refresh page table. } }
here's how add/edit form @ /web-inf/product.jsp
can like:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> ... <form action="product" method="post"> <label for="name">name</label> <input id="name" name="name" value="${fn:escapexml(product.name)}" /> <br/> <label for="description">description</label> <input id="description" name="description" value="${fn:escapexml(product.description)}" /> <br/> <label for="price">price</label> <input id="price" name="price" value="${fn:escapexml(product.price)}" /> <br/> <button type="submit" name="save" value="${product.id}">save</button> </form>
the fn:escapexml()
there prevent xss attacks when edit data redisplayed, same <c:out>
, better suitable usage in attribtues.
here's how product
servlet can (again, conversion/validation omitted brevity):
@webservlet("/product") public class productservlet extends httpservlet { private productdao productdao; // ejb, plain dao, etc. @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string edit = request.getparameter("edit"); if (edit != null) { // edit link clicked? product product = productdao.find(long.valueof(delete)); request.setattribute("product", product); // available ${product} in jsp. } request.getrequestdispatcher("/web-inf/product.jsp").forward(request, response); } @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string save = request.getparameter("save"); if (save != null) { // save button pressed? (note: if empty no product id supplied, means it's "add product". product product = (save.isempty()) ? new product() : productdao.find(long.valueof(save)); product.setname(request.getparameter("name")); product.setdescription(request.getparameter("description")); product.setprice(new bigdecimal(request.getparameter("price"))); productdao.save(product); } response.sendredirect(request.getcontextpath() + "/products"); // go page table. } }
deploy , run it. can open table http://example.com/contextname/products.
see also:
- our servlets wiki page (also contains example validation)
- doget , dopost in servlets
- show jdbc resultset in html in jsp page using mvc , dao pattern
Comments
Post a Comment