演示视频:
代码: https://.com/wu1369955/shopping
收款定制开发购物网站首页
首先说明:收款定制开发这个是花几天搭建出来玩的,从github收款定制开发上拉到找好看的组合的,收款定制开发效果还不错,收款定制开发主要是学习作用.收款定制开发源码之类的也会分享出来,收款定制开发希望一起进步,收款定制开发最好动手实践,收款定制开发可以参照逻辑做的更好,
收款定制开发简易购物商城设计
实验要求: 1.收款定制开发编写注册和登录页面,(收款定制开发要注意检查注册信息是收款定制开发否已经被其他用户使用)。 2.编写采购页面,页面显示不同的商品,(可以按分类来展示,如有能力者可增加商品管理页面,对展示的商品进行管理) 3.用户可以选择商品并加入购物车, 4.用户在购物车可以进行结算产生订单(结算可以不进行实际的支付)。 5.自行设计页面的展示和跳转方式,要求必须登录后才能进入下单页面,不登录只能浏览商品页面。 6.可以查询订单情况(订单样式自行设计)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
一、数据库设计
(1)用户数据表
User表用于存储用户信息
CREATE TABLE IF NOT EXISTS `user`( `user_id` INT UNSIGNED AUTO_INCREMENT, `user_name` VARCHAR(100) NOT NULL, `user_password` VARCHAR(40) NOT NULL, PRIMARY KEY ( `user_id` ))ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO user (user_id, user_name, user_password) VALUES ('1', 'user_1', '123456'), ('2', 'user_2', '123456'), ('3', 'user_3', '123456');insert into user(user_name, user_password) VALUES ('admin@qq.com', '123');select * from user;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
(2)商品数据表
Mall_cloth 表用于存储衣物商品信息
Mall_food 表用于存储食物商品信息
Mall_fruit 表用于存储水果商品信息
Mall_mobile 表用于存储手机商品信息
drop table if exists `mall_food`;drop table if exists `mall_cloth`;drop table if exists `mall_mobile`;drop table if exists `mall_fruit`;create table if not exists `mall_cloth`( `id` int not null , `name` char(20) not null , `price` int not null , `number` int not null, `describe` char(100) not null , PRIMARY KEY (`id`))ENGINE =InnoDB default charset=utf8;create table if not exists `mall_fruit` ( `id` int not null , `name` char(20) not null , `price` int not null , `number` int not null, `describe` char(100) not null , PRIMARY KEY (`id`))ENGINE =InnoDB default charset=utf8;create table if not exists `mall_mobile` ( `id` int not null , `name` char(20) not null , `price` int not null , `number` int not null, `describe` char(100) not null , PRIMARY KEY (`id`))ENGINE =InnoDB default charset=utf8;create table if not exists `mall_food` ( `id` int not null , `name` char(20) not null , `price` int not null , `number` int not null, `describe` char(100) not null , PRIMARY KEY (`id`))ENGINE =InnoDB default charset=utf8;insert into mall_cloth (id, name, price, number, `describe`) VALUES('0','短袖','300','4000','黑色'),('1','长袖','700','2000','黑色'),('2','短裤','800','12000','黑色'),('3','长裤','1000','8000','黑色');insert into mall_fruit (id, name, price, number, `describe`)values ('4','苹果','23','2000','江西'), ('5','香蕉','7','2050','九江'), ('6','荔枝','20','15400','江西'), ('7','橙子','5','8080','赣南');insert into mall_mobile (id, name, price, number, `describe`) VALUES('8','手机','1780','5000','华为'),('9','手表','700','3000','三星'),('10','平板','800','12400','苹果'),('11','电脑','5000','6000','苹果');insert into mall_food (id, name, price, number, `describe`) VALUES('12','三只松鼠','40','785','内地'),('13','老干妈','20','4545','内地'),('14','泡椒鸡爪','10','545','内地'),('15','肥宅快乐水','5','854','内地');
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
(3)购物表单数据表
Cart 表用于存储购物车商品信息
Order表用于存储历史订单商品信息
(4) java 数据设置
java 数据类型
做一个数据库连接接口,使得数据库表和java类数据可以有机连接
(4) 数据接口连接 Database.java
package mysql;import java.sql.*;public class DataBase { private Connection con=null; public DataBase(){ String url="jdbc:mysql://127.0.0.1:3306/shopping?useUnicode=true&characterEncoding=utf-8"; String un="root"; String pwd="123456"; try{ Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection(url, un, pwd); }catch(ClassNotFoundException e){ System.out.println("ClassCastException!!!"); e.printStackTrace(); }catch(SQLException e){ System.out.println("SQLException"); e.printStackTrace(); } } public ResultSet getData(String sql){ Statement stm=null; try{ stm = con.createStatement(); ResultSet result = stm.executeQuery(sql); System.out.println("getData!!!"); return result; }catch(SQLException e){ System.out.println("SQLException!!!"); e.printStackTrace(); return null; } } public void setData(String sql){ Statement stm = null; try{ stm = con.createStatement(); stm.executeUpdate(sql); System.out.println("setData!!!"); }catch(SQLException e){ System.out.println("SQLException!!!"); e.printStackTrace(); }finally { if(stm!=null) { try { stm.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public void close() { try { con.close(); System.out.println("con.close"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
总结:
1.用户表单存储用户信息,验证用户是否登陆,注册.
2.在登陆后,用户拥有选购商品信息的权限,选购的商品将存储在购物车中,
3.同时商品分开显示选购衣物,水果,食品,手机信息,
4.最后,用户可以通过结算生成订单存储在数据库中,用户可以查看历史订单的成交信息
二、前端页面设计
(1)登陆界面设计
1.登陆表单整体布局为登陆头像,提交表单,还有底部显示信息
2.用户通过选择登陆和注册进行用户表的操作
3.注销,用户退出登陆.
4.注册界面设计
(2)表格设计
表格对齐,表格头部进行渲染(css),和各个表格的居中渲染,表单借鉴开源css设计网站的例程,通过一些修改,做成本次得表单由数据库获取的信息来组成表格的内容,整个表格的演示分为商品表格(衣物,食品,水果,手机),和购物车表单,历史记录表单,这些表单都是动态表单.
(3)导航栏设计()
1.导航栏点击主页(主页还有轮播图的效果)
2.单击商品栏水果
3.单击我的 (商品表格) 展示这个商城所有区的商品
4.单击我的(历史记录) ,可以查看历史订单信息
三、后端设计
嗯到这里描述一下主要的逻辑思路:
-
这个是你当前的网页: http://localhost:8082/main.jsp
-
这个是你页面的超链接你点击服装一下这个超链接,链接会变为http://localhost:8082/mall_cloth.goods
-
这个时候web.xml 起作用了,这个*.goods 匹配上了咱们的.GoodsServlet.java 文件,这个时候咱们这个java文件extends HttpServlet ,可以继承doget ,和dopost 方法
- 咱们java类做完相应的处理后,这个时候肯定要退出java了,转到html,或者jsp 页面了,比如resp.sendRedirect(req.getContextPath()+“/login.jsp”),中间的话一些传参,取参数,用到相应的函数.
基本逻辑流程就这样了
(1)Userservlet .java 实现登陆,注册,注销,确认用户是否登陆功能
1.doget 方法实现登陆,注册和确认用户是否登陆的转发
package servlet;import entity.User;import mysql.DataBase;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.IOException;import java.sql.ResultSet;import java.sql.SQLException;public class UserServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String path = req.getServletPath(); resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); if("/login.user".equals(path)){ login(req,resp); } if("/register.user".equals(path)){ register(req,resp); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String path = req.getServletPath(); resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); if("/logout.user".equals(path)){ logout(req,resp); } if("/check_food.user".equals(path)){ check_food(req,resp); } if("/check_fruit.user".equals(path)){ check_fruit(req,resp); } if("/check_mobile.user".equals(path)){ check_mobile(req,resp); } if("/check_cloth.user".equals(path)){ check_cloth(req,resp); } if("/check_mall.user".equals(path)){ check_mall(req,resp); } } protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username=req.getParameter("username"); String password=req.getParameter("password");// String phone=req.getParameter("phone");// String address=req.getParameter("address"); DataBase db=new DataBase(); ResultSet rs = db.getData("SELECT * FROM user where user_name='"+username+"'"); try { if(rs.next()) { req.setAttribute("msg", "用户名已注册,请重新注册!!!"); req.getRequestDispatcher("/register.jsp").forward(req, resp); rs.close(); db.close(); return; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } String sql="insert into user(user_name,user_password) values('"+username+"','"+password+"')"; db.setData(sql); resp.sendRedirect(req.getContextPath()+"/login.jsp"); db.close(); } protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username=req.getParameter("username"); String password=req.getParameter("password"); HttpSession session=req.getSession(); DataBase db=new DataBase(); ResultSet rs = db.getData("SELECT * FROM user where user_name='"+username+"' and user_password='"+password+"'"); System.out.println("调试3"); try { if(rs.next()) { User u=new User(); u.setUsername(rs.getString("user_name")); u.setPassword(rs.getString("user_password"));// u.setPhone(rs.getString(3));// u.setAddress(rs.getString(4)); session.setAttribute("user", u); System.out.println("调试2"); resp.sendRedirect(req.getContextPath()+"/show.goods"); System.out.println("调试1"); return; } req.setAttribute("msg", "用户名或密码错误!!!"); req.getRequestDispatcher("/login.jsp").forward(req, resp); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(rs!=null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } db.close(); } protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session=req.getSession(); session.invalidate(); resp.sendRedirect(req.getContextPath()+"/show.goods"); } protected void check_food(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { User u=(User)req.getSession().getAttribute("user"); if(u==null) { resp.sendRedirect(req.getContextPath()+"/login.jsp"); return; } String tag = req.getParameter("tag"); if("AddCart".equals(tag)) { String index=req.getParameter("index"); resp.sendRedirect(req.getContextPath()+"/add_food.cart?index="+index); } else{ resp.sendRedirect(req.getContextPath()+"/show.cart"); } } protected void check_fruit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { User u=(User)req.getSession().getAttribute("user"); if(u==null) { resp.sendRedirect(req.getContextPath()+"/login.jsp"); return; } String tag = req.getParameter("tag"); if("AddCart".equals(tag)) { String index=req.getParameter("index"); resp.sendRedirect(req.getContextPath()+"/add_fruit.cart?index="+index); } else{ resp.sendRedirect(req.getContextPath()+"/show.cart"); } } protected void check_mobile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { User u=(User)req.getSession().getAttribute("user"); if(u==null) { resp.sendRedirect(req.getContextPath()+"/login.jsp"); return; } String tag = req.getParameter("tag"); if("AddCart".equals(tag)) { String index=req.getParameter("index"); resp.sendRedirect(req.getContextPath()+"/add_mobile.cart?index="+index); } else{ resp.sendRedirect(req.getContextPath()+"/show.cart"); } } protected void check_cloth(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { User u=(User)req.getSession().getAttribute("user"); if(u==null) { resp.sendRedirect(req.getContextPath()+"/login.jsp"); return; } String tag = req.getParameter("tag"); if("AddCart".equals(tag)) { String index=req.getParameter("index"); resp.sendRedirect(req.getContextPath()+"/add_cloth.cart?index="+index); } else{ resp.sendRedirect(req.getContextPath()+"/show.cart"); } } protected void check_mall(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { User u=(User)req.getSession().getAttribute("user"); if(u==null) { resp.sendRedirect(req.getContextPath()+"/login.jsp"); return; } String tag = req.getParameter("tag"); if("Del".equals(tag)) { String mall_name=req.getParameter("mall_name"); String id=req.getParameter("id"); resp.sendRedirect(req.getContextPath()+"/del.mall?mall_name="+mall_name+"&id="+id); } if("Update".equals(tag)){ String mall_name=req.getParameter("mall_name"); String id=req.getParameter("id"); resp.sendRedirect(req.getContextPath()+"/update.mall?mall_name="+mall_name+"&id="+id); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
(2)Cartservlet.java实现购物功能
1.通过.user来跳转确认用户是否登陆,在通过转发来跳转,被Cartservlet 捕获.
Mall_cloth.jsp
Web.xml
单击加入购物车,显示添加成功;
购物车显示如下,可以选择继续购物或者清空购物车
(3)CardServlet实现商品显示功能
package servlet;import entity.Cart;import entity.User;import mysql.DataBase;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;public class CartServlet extends HttpServlet { public static List<Cart> cart=new ArrayList<Cart>(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String path = req.getServletPath(); resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); if("/add_food.cart".equals(path)){ add_food(req,resp); } if("/add_fruit.cart".equals(path)){ add_fruit(req,resp); } if("/add_mobile.cart".equals(path)){ add_mobile(req,resp); } if("/add_cloth.cart".equals(path)){ add_cloth(req,resp); } if("/show.cart".equals(path)){ show(req,resp); } if("/delete.cart".equals(path)){ delete(req,resp); } } protected void add_food(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String index=req.getParameter("index"); User u=(User)req.getSession().getAttribute("user"); String username=u.getUsername(); DataBase db=new DataBase(); ResultSet rs = db.getData("SELECT * FROM mall_food where id="+index); String goodsname = ""; Double price = 0.0; try { if(rs.next()) { goodsname=rs.getString("name"); price=rs.getDouble("price"); } else { System.out.println("获取出错!!!"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } rs = db.getData("SELECT * FROM cart where name='"+goodsname+"' and un='"+username+"'"); try { if(rs.next()) { String sql="UPDATE cart SET number="+(rs.getInt("number")+1)+",price="+(price*(rs.getInt("number")+1))+" where name='"+goodsname+"' and un='"+username+"'"; db.setData(sql); req.setAttribute("msg", "商品"+goodsname+"加入购物成成功!"); req.getRequestDispatcher("/show.goods").forward(req, resp); } else { String sql="insert into cart(name,number,price,un) values('"+goodsname+"',1,'"+price+"','"+username+"')"; System.out.print(sql); db.setData(sql); req.setAttribute("msg", "商品"+goodsname+"加入购物成成功!"); req.getRequestDispatcher("/show_food.goods").forward(req, resp); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(rs!=null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } db.close(); } protected void add_fruit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String index=req.getParameter("index"); User u=(User)req.getSession().getAttribute("user"); String username=u.getUsername(); DataBase db=new DataBase(); ResultSet rs = db.getData("SELECT * FROM mall_fruit where id="+index); String goodsname = ""; Double price = 0.0; try { if(rs.next()) { goodsname=rs.getString("name"); price=rs.getDouble("price"); System.out.println("水果调试1"+goodsname); } else { System.out.println("获取出错!!!"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } rs = db.getData("SELECT * FROM cart where name='"+goodsname+"' and un='"+username+"'"); try { if(rs.next()) { System.out.println("水果调试2"); String sql="UPDATE cart SET number="+(rs.getInt("number")+1)+",price="+(price*(rs.getInt("number")+1))+" where name='"+goodsname+"' and un='"+username+"'"; db.setData(sql); req.setAttribute("msg", "商品"+goodsname+"加入购物成成功!"); req.getRequestDispatcher("/show_fruit.goods").forward(req, resp); } else { String sql="insert into cart(name,number,price,un) values('"+goodsname+"',1,'"+price+"','"+username+"')"; System.out.print(sql); db.setData(sql); req.setAttribute("msg", "商品"+goodsname+"加入购物成成功!"); req.getRequestDispatcher("/show_fruit.goods").forward(req, resp); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(rs!=null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } db.close(); } protected void add_mobile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String index=req.getParameter("index"); User u=(User)req.getSession().getAttribute("user"); String username=u.getUsername(); DataBase db=new DataBase(); ResultSet rs = db.getData("SELECT * FROM mall_mobile where id="+index); String goodsname = ""; Double price = 0.0; try { if(rs.next()) { goodsname=rs.getString("name"); price=rs.getDouble("price"); } else { System.out.println("获取出错!!!"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } rs = db.getData("SELECT * FROM cart where name='"+goodsname+"' and un='"+username+"'"); try { if(rs.next()) { String sql="UPDATE cart SET number="+(rs.getInt("number")+1)+",price="+(price*(rs.getInt("number")+1))+" where name='"+goodsname+"' and un='"+username+"'"; db.setData(sql); req.setAttribute("msg", "商品"+goodsname+"加入购物成成功!"); req.getRequestDispatcher("/show_mobile.goods").forward(req, resp); } else { String sql="insert into cart(name,number,price,un) values('"+goodsname+"',1,'"+price+"','"+username+"')"; System.out.print(sql); db.setData(sql); req.setAttribute("msg", "商品"+goodsname+"加入购物成成功!"); req.getRequestDispatcher("/show_mobile.goods").forward(req, resp); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(rs!=null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } db.close(); } protected void add_cloth(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String index=req.getParameter("index"); User u=(User)req.getSession().getAttribute("user"); String username=u.getUsername(); DataBase db=new DataBase(); ResultSet rs = db.getData("SELECT * FROM mall_cloth where id="+index); String goodsname = ""; Double price = 0.0; try { if(rs.next()) { goodsname=rs.getString("name"); price=rs.getDouble("price"); } else { System.out.println("获取出错!!!"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } rs = db.getData("SELECT * FROM cart where name='"+goodsname+"' and un='"+username+"'"); try { if(rs.next()) { String sql="UPDATE cart SET number="+(rs.getInt("number")+1)+",price="+(price*(rs.getInt("number")+1))+" where name='"+goodsname+"' and un='"+username+"'"; db.setData(sql); req.setAttribute("msg", "商品"+goodsname+"加入购物成成功!"); req.getRequestDispatcher("/show_cloth.goods").forward(req, resp); } else { String sql="insert into cart(name,number,price,un) values('"+goodsname+"',1,'"+price+"','"+username+"')"; System.out.print(sql); db.setData(sql); req.setAttribute("msg", "商品"+goodsname+"加入购物成成功!"); req.getRequestDispatcher("/show_cloth.goods").forward(req, resp); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(rs!=null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } db.close(); } protected void show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { User u=(User)req.getSession().getAttribute("user"); String username=u.getUsername(); DataBase db=new DataBase(); ResultSet rs = db.getData("SELECT * FROM cart where un='"+username+"'"); try { while(rs.next()) { Cart c=new Cart(); c.setGoodsname(rs.getString("name")); c.setNumber(rs.getInt("number")); c.setPrice(rs.getDouble("price")); c.setUsername(username); cart.add(c); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(rs!=null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } req.setAttribute("cart", cart); System.out.println("添加购物车调试接口4"); req.getRequestDispatcher("/cart1.jsp").forward(req, resp); System.out.println("添加购物车调试接口5"); cart.clear(); db.close(); } protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { DataBase db=new DataBase(); String type=req.getParameter("type"); //清空 if("All".equals(type)) { String sql="DELETE FROM cart"; db.setData(sql); resp.sendRedirect(req.getContextPath()+"/show.cart"); } //删除某个 else { String goodsname=req.getParameter("goodsname"); byte[] b=goodsname.getBytes("ISO8859-1"); goodsname=new String(b,"utf-8");// 这里取得的编码是utf-8不做处理,tomcat版本不同返回的值编码可能不一样,如果中文乱码,则对编码进行处理 String sql="DELETE FROM cart WHERE name='"+goodsname+"'"; db.setData(sql); resp.sendRedirect(req.getContextPath()+"/show.cart"); } db.close(); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
1.导航栏超链接如下
2.Web.xml 做一个映射,捕捉.goods 的请求
3.对.good 请求进行一个转化,并在主类中声明主类的数据存储
4.对不同的请求转发进行数据处理数(数据库请求)
5.请求数据库,并将数据进行转发给对应的jsp页面
6.Jsp 页面捕获这些数据进行显示
<%
List goods=(List)request.getAttribute(“goods”);
User u=(User)session.getAttribute(“user”);
%>
(4)OrderServlet.java 订单生成,历史订单查询
购物车页面订单生成,在确认区生成订单,订单数据会存储在数据库中.
查看历史订单:
查看订单信息
同样是路由转发,执行数据库操作,显示数据
package servlet;import entity.Cart;import entity.Order;import entity.User;import mysql.DataBase;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.sql.ResultSet;import java.sql.SQLException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.Random;public class OrderServlet extends HttpServlet { public static List<Cart> cart=new ArrayList<Cart>(); //备份cart 生成订单时用 public static List<Cart> cart_=new ArrayList<Cart>(); //订单号 list public static List<String> OrderId = new ArrayList<String>(); //订单详情 List public static List<Order> order = new ArrayList<Order>(); //时间+随机数 public static String getOrderIdByTime() { SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss"); String newDate=sdf.format(new Date()); String result=""; Random random=new Random(); for(int i=0;i<3;i++) { result+=random.nextInt(10); } return newDate+result; } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String path = req.getServletPath(); resp.setContentType("text/html;charset=utf-8"); req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); if("/confirm.order".equals(path)){ confirm(req,resp); } if("/generate.order".equals(path)){ generate(req,resp); } if("/showOrders.order".equals(path)){ showOrders(req,resp); } if("/detail.order".equals(path)){ detail(req,resp); } } //订单详情 protected void detail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String OrderId=req.getParameter("id"); DataBase db=new DataBase(); ResultSet rs = db.getData("SELECT * FROM order_ where id='"+OrderId+"'"); try { while(rs.next()) { Order o=new Order(); o.setUsername(rs.getString("un")); o.setGoodsname(rs.getString("goodsname")); o.setNumber(rs.getInt("number")); o.setPrice(rs.getDouble("price")); o.setId(OrderId); order.add(o); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(rs!=null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } req.setAttribute("order", order); req.getRequestDispatcher("orderDetail.jsp").forward(req, resp); order.clear(); db.close(); } //查询历史订单 protected void showOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { DataBase db=new DataBase(); User u=(User)req.getSession().getAttribute("user"); ResultSet rs = db.getData("SELECT distinct id FROM order_ where un='"+u.getUsername()+"' "); try { while(rs.next()) { String id=rs.getString("id"); OrderId.add(id); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(rs!=null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } req.setAttribute("OrderId", OrderId); req.getRequestDispatcher("historyOrders.jsp").forward(req, resp); OrderId.clear(); db.close(); } //生成订单 protected void generate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String id=getOrderIdByTime(); User u=(User)req.getSession().getAttribute("user"); String username=u.getUsername(); DataBase db=new DataBase(); for(Cart c:cart_) { String sql="insert into order_(id,un,goodsname,number,price) values('"+id+"','"+username+"','"+c.getGoodsname()+"',"+c.getNumber()+","+c.getPrice()+")"; db.setData(sql); } db.setData("DELETE FROM cart"); req.setAttribute("id", id); req.getRequestDispatcher("success.jsp").forward(req, resp); cart_.clear(); db.close(); } //确认订单 protected void confirm(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { User u=(User)req.getSession().getAttribute("user"); String username=u.getUsername(); DataBase db=new DataBase(); ResultSet rs = db.getData("SELECT * FROM cart where un='"+username+"'"); try { while(rs.next()) { Cart c=new Cart(); c.setGoodsname(rs.getString(1)); c.setNumber(rs.getInt(2)); c.setPrice(rs.getDouble(3)); c.setUsername(username); cart.add(c); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(rs!=null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } req.setAttribute("cart", cart); req.getRequestDispatcher("order.jsp").forward(req, resp); //备份 cart cart_.clear(); cart_.addAll(cart); //清空 cart.clear(); db.close(); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189