步骤 2 : SQL 步骤 3 : Product 步骤 4 : ProductDAO 步骤 5 : ProductListServlet 步骤 6 : listProduct.jsp 步骤 7 : 配置web.xml 步骤 8 : 测试
在eclipse中新建一个java项目,项目名称cart
本章节主要讲解如何开发购物车,所以会把重点放在与购物车紧密相关的查询功能方面,不会全面地提供CRUD(增删改查)功能,如果对CRUD还不够熟悉,请参考 Servlet CRUD
在开始之前,需要做一些数据库方面的准备工作. 1. 创建数据库 cart 2. 在cart 数据库中创建表product 3. 为product准备4条数据 注:插入中文的时候,如果失败,要记得把表的编码方式修改为UTF-8
create database cart;
create database cart;
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product` (
`id` int(11) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`price` float DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `product`; CREATE TABLE `product` ( `id` int(11) DEFAULT NULL, `name` varchar(50) DEFAULT NULL, `price` float DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into product values(1,'黑色的丝袜',500);
insert into product values(2,'充气娃娃',2500);
insert into product values(3,'皮鞭',180);
insert into product values(4,'蜡烛',0.20);
insert into product values(1,'黑色的丝袜',500); insert into product values(2,'充气娃娃',2500); insert into product values(3,'皮鞭',180); insert into product values(4,'蜡烛',0.20);
Product类
package bean;
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;
}
}
ProductDAO提供对Product的查询
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import bean.Product;
public class ProductDAO {
public static void main(String[] args) {
System.out.println(new ProductDAO().ListProduct().size());
}
public List<Product> ListProduct() {
List<Product> products = new ArrayList<Product>();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/cart?characterEncoding=UTF-8",
"root", "admin");
String sql = "select * from product order by id desc";
PreparedStatement ps = c.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Product product = new Product();
int id = rs.getInt(1);
String name = rs.getString(2);
float price = rs.getFloat(3);
product.setId(id);
product.setName(name);
product.setPrice(price);
products.add(product);
}
ps.close();
c.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return products;
}
}
ProductListServlet的作用是通过ProductDAO把product从数据库查出来,然后通过listProduct.jsp显示出来
package 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 bean.Product;
import dao.ProductDAO;
public class ProductListServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Product> products = new ProductDAO().ListProduct();
request.setAttribute("products", products);
request.getRequestDispatcher("listProduct.jsp").forward(request, response);
}
}
显示产品名称,价格,并为每种商品提供一个购买按钮。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
<%@ 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>
进行产品查询Servlet的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>ProductListServlet</servlet-name>
<servlet-class>servlet.ProductListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProductListServlet</servlet-name>
<url-pattern>/listProduct</url-pattern>
</servlet-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <servlet-name>ProductListServlet</servlet-name> <servlet-class>servlet.ProductListServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ProductListServlet</servlet-name> <url-pattern>/listProduct</url-pattern> </servlet-mapping> </web-app>
访问网页
http://127.0.0.1/listProduct 注: 要记得访问路径/listProduct,别直接访问jsp文件 listProduct.jsp 这样就做好进行购买的准备工作,虽然简单,但是足以支持购物车的核心功能了
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
问答区域
2022-06-06
未找到配置的/正在运行的 web 服务器! 请运行任意一个 web 配置并点击“刷新”按钮!
回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
2022-03-10
测试后报错+-
2021-04-08
数据未导进
2020-12-21
我裂开了。显示不出来
2020-08-10
Error instantiating servlet class [servlet.ProductListServlet]
提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 41 条以前的提问,请 点击查看
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|