index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/find">查看所有商品</a>
</body>
</html>
product_list.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" align="center">
<tr>
<td>id</td>
<td>商品名称</td>
<td>单价</td>
<td>描述</td>
</tr>
<c:forEach items="${list}" var="p">
<tr>
<td>${p.id }</td>
<td>${p.pname }</td>
<td>${p.price }</td>
<td>${p.pdesc }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
servlet
package com.xin.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xin.bean.Product;
import com.xin.service.ProductService;
/**
* 查看所有商品
*/
public class FindAllServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//调用service返回list
List<Product> list=null;
list=new ProductService().findAll();
//将list放入request域中
request.setAttribute("list", list);
//请求转发
request.getRequestDispatcher("/product_list.jsp").forward(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
service
package com.xin.service;
import java.util.List;
import com.xin.bean.Product;
import com.xin.dao.ProductDao;
public class ProductService {
public List<Product> findAll(){
return new ProductDao().findAll();
}
}
dao
package com.xin.dao;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.xin.bean.Product;
import com.xin.utils.DataSourceUtils;
public class ProductDao {
public List<Product> findAll(){
QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
//编写sql
String sql="select * from product";
try {
return qr.query(sql,new BeanListHandler<>(Product.class));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
bean
package com.xin.bean;
public class Product {
private int id;
private String pname;
private double price;
private String pdesc;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPdesc() {
return pdesc;
}
public void setPdesc(String pdesc) {
this.pdesc = pdesc;
}
}