基于javaweb+mysql的jsp+servlet简单购物车(java+jsp+servlet+mysql)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
注册、登录、注销
商品加入购物车,查看购物车,删除购物车中的商品和清空购物车
生成订单,查看历史订单及订单详情等
技术框架
JSP Servlet MySQL JDBC Tomcat
基于javaweb+mysql的JSP+Servlet简单购物车(java+jsp+servlet+mysql)
String username=u.getUsername();
DataBase db=new DataBase();
ResultSet rs = db.getData("SELECT * FROM t_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();
}
}
package servlet;
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.cart".equals(path)){
add(req,resp);
}
if("/show.cart".equals(path)){
show(req,resp);
}
if("/delete.cart".equals(path)){
delete(req,resp);
}
}
protected void add(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 t_goods where goodsid="+index);
String goodsname = "";
Double price = 0.0;
try {
if(rs.next()) {
goodsname=rs.getString(2);
price=rs.getDouble(3);
}
else {
System.out.println("获取出错!!!");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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);
@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 t_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();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String sql="insert into t_user(un,pwd,phone,addr) values('"+username+"','"+password+"','"+phone+"','"+address+"')";
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 t_user where un='"+username+"' and pwd='"+password+"'");
try {
if(rs.next()) {
User u=new User();
u.setUsername(rs.getString(1));
u.setPassword(rs.getString(2));
u.setPhone(rs.getString(3));
u.setAddress(rs.getString(4));
session.setAttribute("user", u);
resp.sendRedirect(req.getContextPath()+"/show.goods");
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();
}
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 t_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 t_order(id,un,goodsname,number,price) values('"+id+"','"+username+"','"+c.getGoodsname()+"',"+c.getNumber()+","+c.getPrice()+")";
db.setData(sql);
}
db.setData("DELETE FROM t_cart");
req.setAttribute("id", id);
req.getRequestDispatcher("success.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();
}
}
}
req.setAttribute("cart", cart);
req.getRequestDispatcher("cart.jsp").forward(req, resp);
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 t_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 t_cart WHERE goodsname = '"+goodsname+"' ";
db.setData(sql);
resp.sendRedirect(req.getContextPath()+"/show.cart");
}
db.close();
}
}
package servlet;
}
package servlet;
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.cart".equals(path)){
add(req,resp);
}
if("/show.cart".equals(path)){
show(req,resp);
}
if("/delete.cart".equals(path)){
delete(req,resp);
}
}
protected void add(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 t_goods where goodsid="+index);
String goodsname = "";
Double price = 0.0;
try {
if(rs.next()) {
goodsname=rs.getString(2);
price=rs.getDouble(3);
}
else {
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.user".equals(path)){
check(req,resp);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rs = db.getData("SELECT * FROM t_cart where goodsname='"+goodsname+"' and un='"+username+"'");
try {
if(rs.next()) {
String sql="UPDATE t_cart SET number="+(rs.getInt("number")+1)+",price="+(price*(rs.getInt("number")+1))+" where goodsname='"+goodsname+"' and un='"+username+"'";
db.setData(sql);
req.setAttribute("msg", "商品"+goodsname+"加入购物成成功!");
req.getRequestDispatcher("/show.goods").forward(req, resp);
}
else {
String sql="insert into t_cart(goodsname,number,price,un) values('"+goodsname+"',1,'"+price+"','"+username+"')";
System.out.print(sql);
db.setData(sql);
req.setAttribute("msg", "商品"+goodsname+"加入购物成成功!");
req.getRequestDispatcher("/show.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 t_cart where un='"+username+"'");
try {
while(rs.next()) {
Cart c=new Cart();
c.setGoodsname(rs.getString(1));
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 t_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();
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 t_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("cart.jsp").forward(req, resp);
cart.clear();
db.close();
}
protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
DataBase db=new DataBase();
}
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 t_user where un='"+username+"' and pwd='"+password+"'");
try {
if(rs.next()) {
User u=new User();
u.setUsername(rs.getString(1));
u.setPassword(rs.getString(2));
u.setPhone(rs.getString(3));
u.setAddress(rs.getString(4));
session.setAttribute("user", u);
resp.sendRedirect(req.getContextPath()+"/show.goods");
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(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
User u=(User)req.getSession().getAttribute("user");
if(u==null) {
resp.sendRedirect(req.getContextPath()+"/login.jsp");
return;
req.setAttribute("msg", "商品"+goodsname+"加入购物成成功!");
req.getRequestDispatcher("/show.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 t_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();
}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 t_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
} 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 t_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("cart.jsp").forward(req, resp);
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 t_cart";
db.setData(sql);
public class GoodsServlet extends HttpServlet {
public static List<Goods> goods=new ArrayList<Goods>();
@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("/show.goods".equals(path)){
show(req,resp);
}
}
protected void show(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
DataBase db=new DataBase();
ResultSet rs = db.getData("SELECT * FROM t_goods");
try {
while(rs.next()) {
Goods g=new Goods();
g.setGoodsname(rs.getString(2));
g.setPrice(rs.getDouble(3));
goods.add(g);
}
} 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("goods", goods);
req.getRequestDispatcher("list.jsp").forward(req, resp);
goods.clear();
HttpSession session=req.getSession();
DataBase db=new DataBase();
ResultSet rs = db.getData("SELECT * FROM t_user where un='"+username+"' and pwd='"+password+"'");
try {
if(rs.next()) {
User u=new User();
u.setUsername(rs.getString(1));
u.setPassword(rs.getString(2));
u.setPhone(rs.getString(3));
u.setAddress(rs.getString(4));
session.setAttribute("user", u);
resp.sendRedirect(req.getContextPath()+"/show.goods");
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(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.cart?index="+index);
}
else{
resp.sendRedirect(req.getContextPath()+"/show.cart");
}
}
}
}
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(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.cart?index="+index);
}
else{
resp.sendRedirect(req.getContextPath()+"/show.cart");
}
}
}
package servlet;
// 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(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.cart?index="+index);
}
else{
resp.sendRedirect(req.getContextPath()+"/show.cart");
}
}
}
package servlet;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rs = db.getData("SELECT * FROM t_cart where goodsname='"+goodsname+"' and un='"+username+"'");
try {
if(rs.next()) {
String sql="UPDATE t_cart SET number="+(rs.getInt("number")+1)+",price="+(price*(rs.getInt("number")+1))+" where goodsname='"+goodsname+"' and un='"+username+"'";
db.setData(sql);
req.setAttribute("msg", "商品"+goodsname+"加入购物成成功!");
req.getRequestDispatcher("/show.goods").forward(req, resp);
}
else {
String sql="insert into t_cart(goodsname,number,price,un) values('"+goodsname+"',1,'"+price+"','"+username+"')";
System.out.print(sql);
db.setData(sql);
req.setAttribute("msg", "商品"+goodsname+"加入购物成成功!");
req.getRequestDispatcher("/show.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();
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();
}
}
package servlet;
public class CartServlet extends HttpServlet {
public static List<Cart> cart=new ArrayList<Cart>();
@Override
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 t_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 t_order(id,un,goodsname,number,price) values('"+id+"','"+username+"','"+c.getGoodsname()+"',"+c.getNumber()+","+c.getPrice()+")";
db.setData(sql);
}
db.setData("DELETE FROM t_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");
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 t_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();
}
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)){