因为本章节主要讲解如何开发一个购物车,所以产品部分,只提供与购物车相关的产品查询功能。不提供全面的增加,删除,修改,以免分散学习的注意力。
步骤1:新建一个项目
步骤2:SQL
步骤3:Product
步骤4:ProductDAO
步骤5:ProductListServlet
步骤6:listProduct.jsp
步骤7:配置web.xml
步骤8:测试
步骤 1 : 新建一个项目
在eclipse中新建一个java项目,项目名称cart
步骤 2 : SQL
本章节主要讲解如何开发购物车,所以会把重点放在与购物车紧密相关的查询功能方面,不会全面地提供CRUD(增删改查)功能,如果对CRUD还不够熟悉,请参考 Servlet CRUD
在开始之前,需要做一些数据库方面的准备工作.
1. 创建数据库 cart
2. 在cart 数据库中创建表product
3. 为product准备4条数据
注:插入中文的时候,如果失败,要记得把表的编码方式修改为UTF-8
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);
|
步骤 3 : Product
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;
}
}
|
步骤 4 : ProductDAO
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;
}
}
|
步骤 5 : ProductListServlet
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);
}
}
|
步骤 6 : listProduct.jsp
显示产品名称,价格,并为每种商品提供一个购买按钮。
<%@ 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>
|
步骤 7 : 配置web.xml
进行产品查询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 >
|
步骤 8 : 测试
访问网页
http: //127.0.0.1/listProduct
|
注: 要记得访问路径/listProduct,别直接访问jsp文件 listProduct.jsp
这样就做好进行购买的准备工作,虽然简单,但是足以支持购物车的核心功能了
更多内容,点击了解: https://how2j.cn/k/cart/cart-product/596.html