综合练习-产品模块
2020-06-21 本文已影响0人
通灵路耳
总图:先些产品,再写用户,然后是订单项,最后写订单
图片.png
代码
百度网盘:
链接:https://pan.baidu.com/s/13yIlRuOmRd-_OSnKMnfl5A
提取码:7nr3
1、导入jar包
2、配置web.xml
<servlet>
<servlet-name>ProductListServlet</servlet-name>
<servlet-class>com.llhc.web.ProductListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProductListServlet</servlet-name>
<url-pattern>/listProduct</url-pattern>
</servlet-mapping>
3、配置model
package com.llhc.model;
public class Product {
private int id;
private String name;
private float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
4、配置dao层
package com.llhc.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.llhc.model.Product;
public class ProductDao {
public List<Product> ListProduct(){
//产品查询
List<Product> products = new ArrayList<Product>();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db_web_cart?characterEncoding=UTF-8","root","root");
String sql = "select * from product";
PreparedStatement p=conn.prepareStatement(sql);
ResultSet rs=p.executeQuery();
while(rs.next()){
Product product = new Product();
product.setId(rs.getInt("id"));
product.setName(rs.getString("name"));
product.setPrice(rs.getFloat("price"));
products.add(product);
}
} catch (Exception e) {
// TODO: handle exception
}
return products;
}
}
5、配置servlet
package com.llhc.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.llhc.dao.ProductDao;
import com.llhc.model.Product;
public class ProductServlet extends HttpServlet{
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req,resp);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
List<Product> list= new ProductDao().ListProduct();
System.out.println(list);
req.setAttribute("products", list);
req.getRequestDispatcher("listProduct.jsp").forward(req, resp);
}
}
6、前端页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>名称</td>
<td>价格</td>
<td>购买</td>
</tr>
<c:forEach items="${products}" var="product" varStatus="st">
<tr>
<td>${product.id}</td>
<td>${product.name}</td>
<td>${product.price}</td>
<td>
<form action="addOrderItem" method="post">
数量<input type="text" value="1" name="num">
<input type="hidden" name="pid" value="${product.id}">
<input type="submit" value="购买">
</form>
</td>
</tr>
</c:forEach>
</table>
7、访问:http://localhost/Cart/listProduct
图片.png