演示视频:
购物网站
代码: https://github.com/wu1369955/shopping
购物网站首页
首先说明:这个是花几天搭建出来玩的,从github上拉到找好看的框架组合的,效果还不错,主要是学习作用.源码之类的也会分享出来,希望一起进步,最好动手实践,可以参照逻辑做的更好,
实验要求:
1.编写注册和登录页面,(要注意检查注册信息是否已经被其他用户使用)。
2.编写采购页面,页面显示不同的商品,(可以按分类来展示,如有能力者可增加商品管理页面,对展示的商品进行管理)
3.用户可以选择商品并加入购物车,
4.用户在购物车可以进行结算产生订单(结算可以不进行实际的支付)。
5.自行设计页面的展示和跳转方式,要求必须登录后才能进入下单页面,不登录只能浏览商品页面。
6.可以查询订单情况(订单样式自行设计)
一、数据库设计
(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;
(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','内地');
(3)购物表单数据表
Cart 表用于存储购物车商品信息
Order表用于存储历史订单商品信息
做一个数据库连接接口,使得数据库表和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.最后,用户可以通过结算生成订单存储在数据库中,用户可以查看历史订单的成交信息
1.登陆表单整体布局为登陆头像,提交表单,还有底部显示信息
2.用户通过选择登陆和注册进行用户表的 *** 作
3.注销,用户退出登陆.
4.注册界面设计
表格对齐,表格头部进行渲染(css),和各个表格的居中渲染,表单借鉴开源css设计网站的例程,通过一些修改,做成本次得表单由数据库获取的信息来组成表格的内容,整个表格的演示分为商品表格(衣物,食品,水果,手机),和购物车表单,历史记录表单,这些表单都是动态表单.
(3)导航栏设计()1.导航栏点击主页(主页还有轮播图的效果)
2.单击商品栏水果
3.单击我的 (商品表格) 展示这个商城所有区的商品
4.单击我的(历史记录) ,可以查看历史订单信息
三、后端设计嗯到这里描述一下主要的逻辑思路:
-
这个是你当前的网页: http://localhost:8082/main.jsp
-
这个是你页面的超链接你点击服装一下这个超链接,链接会变为http://localhost:8082/mall_cloth.goods
-
这个时候web.xml 起作用了,这个*.goods 匹配上了咱们的servlet.GoodsServlet.java 文件,这个时候咱们这个java文件extends HttpServlet ,可以继承doget ,和dopost 方法
- 咱们java类做完相应的处理后,这个时候肯定要退出java了,转到html,或者jsp 页面了,比如resp.sendRedirect(req.getContextPath()+“/login.jsp”),中间的话一些传参,取参数,用到相应的函数.
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);
}
}
}
(2)Cartservlet.java实现购物功能
1.通过.user来跳转确认用户是否登陆,在通过转发来跳转,被Cartservlet 捕获.
Mall_cloth.jsp
Web.xml
单击加入购物车,显示添加成功;
购物车显示如下,可以选择继续购物或者清空购物车
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.Web.xml 做一个映射,捕捉.goods 的请求
3.对.good 请求进行一个转化,并在主类中声明主类的数据存储
4.对不同的请求转发进行数据处理数(数据库请求)
5.请求数据库,并将数据进行转发给对应的jsp页面
6.Jsp 页面捕获这些数据进行显示
<%
List goods=(List)request.getAttribute(“goods”);
User u=(User)session.getAttribute(“user”);
%>
购物车页面订单生成,在确认区生成订单,订单数据会存储在数据库中.
查看历史订单:
查看订单信息
同样是路由转发,执行数据库 *** 作,显示数据
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();
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)