java - Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern -
i'm implementing mvc using jsp , jdbc. have imported database class file jsp file , show data of db table. don't know how should return resultset
java class jsp page , embed in html.
how can achieve this?
in designed mvc approach, jsp file should not contain line of java code , servlet class should not contain line of jdbc code.
assuming want show list of products in webshop, following code needs created.
a
product
class representing real world entity of product, should javabean.public class product { private long id; private string name; private string description; private bigdecimal price; // add/generate getters/setters/c'tors/equals/hashcode boilerplate. }
a dao class nasty jdbc work , returns nice
list<product>
.public class productdao { private datasource datasource; public productdao(datasource datasource) { this.datasource = datasource; } public list<product> list() throws sqlexception { list<product> products = new arraylist<product>(); try ( connection connection = datasource.getconnection(); preparedstatement statement = connection.preparestatement("select id, name, description, price product"); resultset resultset = statement.executequery(); ) { while (resultset.next()) { product product = new product(); product.setid(resultset.getlong("id")); product.setname(resultset.getstring("name")); product.setdescription(resultset.getstring("description")); product.setprice(resultset.getbigdecimal("price")); products.add(product); } } return products; } }
a servlet class obtains list , puts in request scope.
@webservlet("/products") public class productsservlet extends httpservlet { @resource("jdbc/yourdb") // tomcat, define <resource> in context.xml , declare <resource-ref> in web.xml. private datasource datasource; private productdao productdao; @override public void init() { productdao = new productdao(datasource); } @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { try { list<product> products = productdao.list(); request.setattribute("products", products); // available ${products} in jsp request.getrequestdispatcher("/web-inf/products.jsp").forward(request, response); } catch (sqlexception e) { throw new servletexception("cannot obtain products db", e); } } }
finally jsp file in
/web-inf/products.jsp
uses jstl<c:foreach>
iterate onlist<product>
made available in el${products}
, , uses jstl<c:out>
escape string properties in order avoid xss holes when concerns user-controlled input.<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/format" prefix="fmt" %> ... <table> <c:foreach items="${products}" var="product"> <tr> <td>${product.id}</td> <td><c:out value="${product.name}" /></td> <td><c:out value="${product.description}" /></td> <td><fmt:formatnumber value="${product.price}" type="currency" currencycode="usd" /></td> </tr> </c:foreach> </table>
to work, call servlet url. provided servlet annotated @webservlet("/products")
or mapped in web.xml
<url-pattern>/products</url-pattern>
, can call http://example.com/contextname/products
see also:
- how avoid java code in jsp files?
- doget , dopost in servlets
- how should connect jdbc database / datasource in servlet based application?
- design patterns web based applications
- requestdispatcher.forward() vs httpservletresponse.sendredirect()
- how map resultset unknown amount of columns list , display in html table?
- how pass current item java method clicking hyperlink or button in jsp page?
Comments
Post a Comment