database - Populating a JSP page with contents from client -
i have server hosting jsp page. can populate it's text boxes client's database?
create servlet loads data, puts in request scope , forwards request jsp. if want whenever client opens link/bookmark, in doget()
method. or when want when client submits form, in dopost()
method.
here's example preloads specific product db based on request parameter:
product product = productservice.find(request.getparameter("id")); // db access job. request.setattribute("product", product); // it'll available ${product}. request.getrequestdispatcher("/web-inf/product.jsp").forward(request, response); // let jsp display it.
map servlet on url pattern of /product
you'll able call http://example.com/somecontext/product?id=123
in jsp have set value
attribute of html input element display value of input element. since sensitive xss attacks when print plain suggested in other answer, you'd use jstl fn:escapexml()
avoid xss attacks.
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> ... <input type="text" name="name" value="${fn:escapexml(product.name)}" /> <input type="text" name="description" value="${fn:escapexml(product.description)}" /> <input type="text" name="price" value="${fn:escapexml(product.price)}" />
note scriptlets (those <% %>
things) poor practice , don't offer instant access request attributes (i.e. <%= product.getname() %>
style suggested in other answer won't work), nor offer standard xss escaping facility.
Comments
Post a Comment