基于javaweb+mysql的jsp+servlet电影院售票管理系统(java+servlet+jsp+jdbc+mysql)

本文介绍基于JavaWeb和MySQL的JSP+Servlet电影院售票管理系统。该系统分管理员和会员两种角色,有不同权限。运行环境需Java≥8、MySQL≥5.7等,开发工具多样。还说明了技术栈、使用方法,适用于课程设计等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于javaweb+mysql的jsp+servlet电影院售票管理系统(java+servlet+jsp+jdbc+mysql)

运行环境

Java≥8、MySQL≥5.7、Tomcat≥8

开发工具

eclipse/idea/myeclipse/sts等均可配置运行

适用

课程设计,大作业,毕业设计,项目练习,学习演示等

功能说明

基于javaweb+mysql的JSP+Servlet电影院售票管理系统(java+servlet+jsp+jdbc+mysql)

项目介绍

这个项目是一个基于Servlet+JSP的电影院售票系统,分为管理员和会员两种角色。

管理员权限包括:

修改登录密码 电影类别管理 电影信息管理 会员信息管理 订单信息管理 留言信息管理

会员权限包括:

会员注册 会员登录 浏览电影 我的购物车 我的订单 留言板

环境需要

1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。 2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA; 3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可 4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 5.数据库:MySql 5.7版本; 6.是否Maven项目: 否;查看源码目录中是否包含pom.xml;若包含,则为maven项目,否则为非maven项目

技术栈

Servlet、JSP、JDBC、MySQL5.7、Tomcat8

使用说明

  1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件; 2. 使用IDEA/Eclipse/MyEclipse导入项目,然后运行; 3. 将项目中src/com/dao/DB.java配置文件中的数据库配置改为自己的配置; 4. 运行项目,在浏览器中输入localhost:8080/goupiao 管理员账号/密码:admin/admin 会员账号/密码:zhangsan/123456
			Object[] params={id,loginname,loginpw,name,del};
			DB mydb=new DB();
			mydb.doPstm(sql, params);
			mydb.closed();
			
			req.setAttribute("message", "注册成功,请登录");
			req.setAttribute("path", "site/default.jsp");
			
	        String targetURL = "/common/success.jsp";
			dispatch(targetURL, req, res);
		}
		
		
	}
	
	
	
	public void userLogout(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
		HttpSession session=req.getSession();
		session.setAttribute("userType", null);
		session.setAttribute("user", null);
		
			
		req.setAttribute("message", "成功退出系统");
		req.setAttribute("path", "site/default.jsp");
		String targetURL = "/common/success.jsp";
		dispatch(targetURL, req, res);
	}
	
	
	public void userEditMe(HttpServletRequest req,HttpServletResponse res)
	{
		String id=req.getParameter("id");
		String loginname=req.getParameter("loginname");
		String loginpw=req.getParameter("loginpw");
		String name=req.getParameter("name");
		
		String sql="update t_user set loginname=?,loginpw=?,name=? where id=?";
		Object[] params={loginname,loginpw,name,id};
		DB mydb=new DB();
		mydb.doPstm(sql, params);
		mydb.closed();
		
		req.setAttribute("message", "修改成功,重新等后生效");
		req.setAttribute("path", "site/default.jsp");
		
        String targetURL = "/common/success.jsp";
		dispatch(targetURL, req, res);
		
		
		req.setAttribute("msg", "操作成功");
		String targetURL = "/common/msg.jsp";
		dispatch(targetURL, req, res);
	}
	
	
	public void goodsMana(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
		List goodsList=new ArrayList();
		String sql="select * from t_goods where del='no' order by fangyingshi desc";
		Object[] params={};
		DB mydb=new DB();
		try
		{
			mydb.doPstm(sql, params);
			ResultSet rs=mydb.getRs();
			while(rs.next())
			{
				Tgoods goods=new Tgoods();
				
				goods.setId(rs.getString("id"));
				goods.setFangyingshi(rs.getString("fangyingshi"));
				goods.setCatelog_id(rs.getString("catelog_id"));
				
				goods.setMingcheng(rs.getString("mingcheng"));
				goods.setJieshao(rs.getString("jieshao"));
				goods.setFujian(rs.getString("fujian"));
				
				goods.setShichangjia(rs.getInt("shichangjia"));
				goods.setTejia(rs.getInt("tejia"));
				goods.setDel(rs.getString("del"));
				
				goodsList.add(goods);
		    }
			rs.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		mydb.closed();
		
		req.setAttribute("goodsList", goodsList);
		req.getRequestDispatcher("admin/goods/goodsMana.jsp").forward(req, res);
	}
	
	
	
	public void goodsDel(HttpServletRequest req,HttpServletResponse res)
	{
		String id=req.getParameter("id");
		String sql="update t_goods set del='yes' where id="+id;

public class loginService
{
	public String login(String userName,String userPw,int userType)
	{
		String result="no";
		
		if(userType==0)//系统管理员登陆
		{
			String sql="select * from t_admin where userName=? and userPw=?";
			Object[] params={userName,userPw};
			DB mydb=new DB();
			mydb.doPstm(sql, params);
			try 
			{
				ResultSet rs=mydb.getRs();
				boolean mark=(rs==null||!rs.next()?false:true);
				if(mark==false)
				{
					 result="no";
				}
				else
				{
					 result="yes";
					 TAdmin admin=new TAdmin();
					 admin.setUserId(rs.getInt("userId"));
					 admin.setUserName(rs.getString("userName"));
					 admin.setUserPw(rs.getString("userPw"));
					 WebContext ctx = WebContextFactory.get(); 
					 HttpSession session=ctx.getSession(); 
					 session.setAttribute("userType", 0);
		             session.setAttribute("admin", admin);
				}
				rs.close();
			} 
			catch (SQLException e)
			{
				System.out.println("登录失败!");
				e.printStackTrace();
			}
			finally

public class catelog_servlet extends HttpServlet
{
	public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException 
	{
        String type=req.getParameter("type");
		
		if(type.endsWith("catelogAdd"))
		{
			catelogAdd(req, res);
		}
		if(type.endsWith("catelogMana"))
		{
			catelogMana(req, res);
		}
		if(type.endsWith("catelogDel"))
		{
			catelogDel(req, res);
		}
	}
	
	public void catelogAdd(HttpServletRequest req,HttpServletResponse res)
	{
		String id=String.valueOf(new Date().getTime());
		String name=req.getParameter("name").trim();
		String del="no";
		
		String sql="insert into t_catelog(id,name,del) values(?,?,?)";
		Object[] params={id,name,del};
		DB mydb=new DB();
		mydb.doPstm(sql, params);
		mydb.closed();
		
		req.setAttribute("msg", "操作成功");
		String targetURL = "/common/msg.jsp";
		dispatch(targetURL, req, res);
	}
	
	
	public void catelogMana(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
		List catelogList=new ArrayList();
				goods.setJieshao(rs.getString("jieshao"));
				goods.setFujian(rs.getString("fujian"));
				
				goods.setShichangjia(rs.getInt("shichangjia"));
				goods.setTejia(rs.getInt("tejia"));
				goods.setDel(rs.getString("del"));
				
				goodsList.add(goods);
		    }
			rs.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		mydb.closed();
		
		if(goodsList.size()>8)
		{
			goodsList=goodsList.subList(0, 8);
		}
		
		req.setAttribute("goodsList", goodsList);
		req.getRequestDispatcher("site/goods/goodsAll.jsp").forward(req, res);
	}
	
	
	
	
	public void goodsByCatelog(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
        String catelog_id=req.getParameter("catelog_id");
		
		req.setAttribute("goodsList", liuService.goodsByCatelog(catelog_id));
		req.getRequestDispatcher("site/goods/goodsByCatelog.jsp").forward(req, res);
	}
	
	public void goodsDetailQian(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
		String id=req.getParameter("id");
		
		req.setAttribute("goods", liuService.getGoods(id));
		req.getRequestDispatcher("site/goods/goodsDetailQian.jsp").forward(req, res);
			 result="yes";
		/*}*/
		return result;
	}
	
	public String delGoodsFromCart(String goodsId)
	{
		 WebContext ctx = WebContextFactory.get(); 
		 HttpSession session=ctx.getSession();
		 Cart cart=(Cart)session.getAttribute("cart");
		 cart.delGoods(goodsId);
		 session.setAttribute("cart", cart);
		 return "yes";
	}
	
	
	public String clearCart()
	{
		 WebContext ctx = WebContextFactory.get(); 
		 HttpSession session=ctx.getSession();
		 Cart cart=(Cart)session.getAttribute("cart");
		 cart.getItems().clear();
		 session.setAttribute("cart", cart);
		 return "yes";
	}
	
}

public class index_servlet extends HttpServlet
{
	public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException 
	{
        req.getSession().setAttribute("catelogList", liuService.catelogList());
        req.getRequestDispatcher("site/index.jsp").forward(req, res);
		
		for (Iterator it = cart.getItems().values().iterator(); it.hasNext();)
		{

			TorderItem orderItem = (TorderItem) it.next();
			
			String id=String.valueOf(new Date().getTime());
			String order_id=order.getId();
			String goods_id=orderItem.getGoods().getId();
			int goods_quantity=orderItem.getGoods_quantity();
			liuService.saveOrderItem(id, order_id, goods_id, goods_quantity);
			
			liuService.updateGoodsKucun(goods_id, goods_quantity);
		}
		
		cart.getItems().clear();
		session.setAttribute("cart", cart);
		
		req.setAttribute("order", order);
		req.getRequestDispatcher("site/order/orderSubmit.jsp").forward(req, res);
	}
	
	
	public void myorder(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
		HttpSession session=req.getSession();
		Tuser user=(Tuser)session.getAttribute("user");
		
		req.setAttribute("orderList", liuService.orderList(user.getId()));
		req.getRequestDispatcher("site/order/myorder.jsp").forward(req, res);
	}
	
	public void orderDetail(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
		String order_id=req.getParameter("order_id");	
		System.out.println(order_id+"DD");
		req.setAttribute("orderItemList", liuService.orderItemList(order_id));
		req.getRequestDispatcher("site/order/orderDetail.jsp").forward(req, res);
	}
	
	
	public void dispatch(String targetURI,HttpServletRequest request,HttpServletResponse response) 
	{
		RequestDispatcher dispatch = getServletContext().getRequestDispatcher(targetURI);
		try 
		{
		    dispatch.forward(request, response);
		    return;
		} 
				user.setLoginpw(rs.getString("loginpw"));
				user.setLoginpw(rs.getString("loginpw"));
				user.setName(rs.getString("name"));
				
				userList.add(user);
		    }
			rs.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		mydb.closed();
		
		req.setAttribute("userList", userList);
		req.getRequestDispatcher("admin/user/userMana.jsp").forward(req, res);
	}
	
	
	
	public void userDel(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
        String id=req.getParameter("id");
		
		String sql="update t_user set del='yes' where id=?";
		Object[] params={id};
		DB mydb=new DB();
		mydb.doPstm(sql, params);
		mydb.closed();
		
		req.setAttribute("msg", "用户信息删除完毕");
		String targetURL = "/common/msg.jsp";
		dispatch(targetURL, req, res);
	}
	
	
	public void dispatch(String targetURI,HttpServletRequest request,HttpServletResponse response) 
	{
		RequestDispatcher dispatch = getServletContext().getRequestDispatcher(targetURI);
		try 
		{
		    dispatch.forward(request, response);
		    return;
		} 
			goodsByCatelog(req, res);
		}
		if(type.endsWith("goodsDetailQian"))
		{
			goodsDetailQian(req, res);
		}
	}
	
	public void goodsAdd(HttpServletRequest req,HttpServletResponse res)
	{
		String id=String.valueOf(new Date().getTime());
		String fangyingshi=req.getParameter("fangyingshi");
		String catelog_id=req.getParameter("catelog_id");
		String mingcheng=req.getParameter("mingcheng");
		
		String jieshao=req.getParameter("jieshao");
		String fujian=req.getParameter("fujian");
		int shichangjia=Integer.parseInt(req.getParameter("shichangjia"));
		int tejia=Integer.parseInt(req.getParameter("shichangjia"));

		String del="no";
		
		String sql="insert into t_goods(id,fangyingshi,catelog_id,mingcheng,jieshao,fujian,shichangjia,tejia,del) " +
				   "values(?,?,?,?,?,?,?,?,?)";
		Object[] params={id,fangyingshi,catelog_id,mingcheng,jieshao,fujian,shichangjia,tejia,del};
		DB mydb=new DB();
		mydb.doPstm(sql, params);
		mydb.closed();
		
		req.setAttribute("msg", "操作成功");
		String targetURL = "/common/msg.jsp";
		dispatch(targetURL, req, res);
	}
	
	
	public void goodsMana(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
		List goodsList=new ArrayList();
		String sql="select * from t_goods where del='no' order by fangyingshi desc";
		Object[] params={};
		mydb.closed();
		
		req.setAttribute("msg", "留言信息删除完毕");
        String targetURL = "/common/msg.jsp";
		dispatch(targetURL, req, res);
	}
	
	
	public void liuyanHuifu(HttpServletRequest req,HttpServletResponse res)
	{
		String huifu=req.getParameter("huifu");
		String huifushi=new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
		int id=Integer.parseInt(req.getParameter("id"));
		
		String sql="update t_liuyan set huifu=?,huifushi=? where id=?";
		Object[] params={huifu,huifushi,id};
		DB mydb=new DB();
		mydb.doPstm(sql, params);
		mydb.closed();
		
		req.setAttribute("msg", "回复完毕完毕");
        String targetURL = "/common/msg.jsp";
		dispatch(targetURL, req, res);
	}

	public void liuyanAll(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
		List liuyanList=new ArrayList();
		String sql="select * from t_liuyan order by liuyanshi";
		Object[] params={};
		DB mydb=new DB();
		try
		{
			mydb.doPstm(sql, params);
			ResultSet rs=mydb.getRs();
			while(rs.next())
			{
				TLiuyan liuyan=new TLiuyan();
				
				liuyan.setId(rs.getInt("id"));
				liuyan.setNeirong(rs.getString("neirong"));
				liuyan.setLiuyanshi(rs.getString("liuyanshi"));
				liuyan.setUser_id(rs.getString("user_id"));
				
				liuyan.setHuifu(rs.getString("huifu"));
				liuyan.setHuifushi(rs.getString("huifushi"));
			
				liuyanList.add(liuyan);
		    }
			rs.close();
		}
	{
		String sql="delete from t_liuyan where id="+Integer.parseInt(req.getParameter("id"));
		Object[] params={};
		DB mydb=new DB();
		mydb.doPstm(sql, params);
		mydb.closed();
		
		req.setAttribute("msg", "留言信息删除完毕");
        String targetURL = "/common/msg.jsp";
		dispatch(targetURL, req, res);
	}
	
	
	public void liuyanHuifu(HttpServletRequest req,HttpServletResponse res)
	{
		String huifu=req.getParameter("huifu");
		String huifushi=new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
		int id=Integer.parseInt(req.getParameter("id"));
		
		String sql="update t_liuyan set huifu=?,huifushi=? where id=?";
		Object[] params={huifu,huifushi,id};
		DB mydb=new DB();
		mydb.doPstm(sql, params);
		mydb.closed();
		
		req.setAttribute("msg", "回复完毕完毕");
        String targetURL = "/common/msg.jsp";
		dispatch(targetURL, req, res);
	}

	public void liuyanAll(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
		List liuyanList=new ArrayList();
		String sql="select * from t_liuyan order by liuyanshi";
		Object[] params={};
		DB mydb=new DB();
		try
		{
			mydb.doPstm(sql, params);
			ResultSet rs=mydb.getRs();
			while(rs.next())
		String s=liuService.panduan_zhanghao(loginname);
		if(s.equals("yizhan"))
		{
			req.setAttribute("message", "账号已被占用,请输入其他账号");
			req.setAttribute("path", "site/userreg/userreg.jsp");
			
	        String targetURL = "/common/success.jsp";
			dispatch(targetURL, req, res);
		}
		else
		{
			String sql="insert into t_user values(?,?,?,?,?)";
			Object[] params={id,loginname,loginpw,name,del};
			DB mydb=new DB();
			mydb.doPstm(sql, params);
			mydb.closed();
			
			req.setAttribute("message", "注册成功,请登录");
			req.setAttribute("path", "site/default.jsp");
			
	        String targetURL = "/common/success.jsp";
			dispatch(targetURL, req, res);
		}
		
		
	}
	
	
	
	public void userLogout(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
		HttpSession session=req.getSession();
		session.setAttribute("userType", null);
		session.setAttribute("user", null);
		
			
		req.setAttribute("message", "成功退出系统");
		req.setAttribute("path", "site/default.jsp");
		String targetURL = "/common/success.jsp";
		dispatch(targetURL, req, res);
	}
	
	
	public void userEditMe(HttpServletRequest req,HttpServletResponse res)
	{
		String id=req.getParameter("id");
		String loginname=req.getParameter("loginname");
		String loginpw=req.getParameter("loginpw");
		String name=req.getParameter("name");
		
				goods.setCatelog_id(rs.getString("catelog_id"));
				
				goods.setMingcheng(rs.getString("mingcheng"));
				goods.setJieshao(rs.getString("jieshao"));
				goods.setFujian(rs.getString("fujian"));
				
				goods.setShichangjia(rs.getInt("shichangjia"));
				goods.setTejia(rs.getInt("tejia"));
				goods.setDel(rs.getString("del"));
				
				goodsList.add(goods);
		    }
			rs.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		mydb.closed();
		
		req.setAttribute("goodsList", goodsList);
		req.getRequestDispatcher("admin/goods/goodsMana.jsp").forward(req, res);
	}
	
	
	
	public void goodsDel(HttpServletRequest req,HttpServletResponse res)
	{
		String id=req.getParameter("id");
		String sql="update t_goods set del='yes' where id="+id;
		Object[] params={};
		DB mydb=new DB();
		mydb.doPstm(sql, params);
		mydb.closed();
		
		req.setAttribute("msg", "操作成功");
		String targetURL = "/common/msg.jsp";
		dispatch(targetURL, req, res);
	}
	
	
	public void goodsDetailHou(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
        String id=req.getParameter("id");
		
		req.setAttribute("goods", liuService.getGoods(id));
		req.getRequestDispatcher("admin/goods/goodsDetailHou.jsp").forward(req, res);
	}
	
	
	public void goodsAll(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
		List goodsList=new ArrayList();
		//String sql="select * from t_goods where del='no' and fangyingshi >? order by fangyingshi desc";
	}
	
	public void destroy() 
	{
		
	}
}

public class user_servlet extends HttpServlet
{
	
	public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException 
	{
        String type=req.getParameter("type");
		
		if(type.endsWith("userReg"))
		{
			userReg(req, res);
		}
		if(type.endsWith("userLogout"))
		{
			userLogout(req, res);
		}
		
		if(type.endsWith("userEditMe"))
		{
			userEditMe(req, res);
		}
		if(type.endsWith("userMana"))
		{
		String targetURL = "/common/msg.jsp";
		dispatch(targetURL, req, res);
	}
	
	
	public void goodsMana(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
		List goodsList=new ArrayList();
		String sql="select * from t_goods where del='no' order by fangyingshi desc";
		Object[] params={};
		DB mydb=new DB();
		try
		{
			mydb.doPstm(sql, params);
			ResultSet rs=mydb.getRs();
			while(rs.next())
			{
				Tgoods goods=new Tgoods();
				
				goods.setId(rs.getString("id"));
				goods.setFangyingshi(rs.getString("fangyingshi"));
				goods.setCatelog_id(rs.getString("catelog_id"));
				
				goods.setMingcheng(rs.getString("mingcheng"));
				goods.setJieshao(rs.getString("jieshao"));
				goods.setFujian(rs.getString("fujian"));
				
				goods.setShichangjia(rs.getInt("shichangjia"));
				goods.setTejia(rs.getInt("tejia"));
				goods.setDel(rs.getString("del"));
				
				goodsList.add(goods);
		    }
			rs.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		mydb.closed();
		
		req.setAttribute("goodsList", goodsList);
		req.getRequestDispatcher("admin/goods/goodsMana.jsp").forward(req, res);
	}
	
	
	
	public void goodsDel(HttpServletRequest req,HttpServletResponse res)
	{
		String id=req.getParameter("id");
		String sql="update t_goods set del='yes' where id="+id;
		Object[] params={};
		DB mydb=new DB();
		if(type.endsWith("goodsDetailQian"))
		{
			goodsDetailQian(req, res);
		}
	}
	
	public void goodsAdd(HttpServletRequest req,HttpServletResponse res)
	{
		String id=String.valueOf(new Date().getTime());
		String fangyingshi=req.getParameter("fangyingshi");
		String catelog_id=req.getParameter("catelog_id");
		String mingcheng=req.getParameter("mingcheng");
		
		String jieshao=req.getParameter("jieshao");
		String fujian=req.getParameter("fujian");
		int shichangjia=Integer.parseInt(req.getParameter("shichangjia"));
		int tejia=Integer.parseInt(req.getParameter("shichangjia"));

		String del="no";
		
		String sql="insert into t_goods(id,fangyingshi,catelog_id,mingcheng,jieshao,fujian,shichangjia,tejia,del) " +
				   "values(?,?,?,?,?,?,?,?,?)";
		Object[] params={id,fangyingshi,catelog_id,mingcheng,jieshao,fujian,shichangjia,tejia,del};
		DB mydb=new DB();
		mydb.doPstm(sql, params);
		mydb.closed();
		
		req.setAttribute("msg", "操作成功");
		String targetURL = "/common/msg.jsp";
		dispatch(targetURL, req, res);
	}
	
	
	public void goodsMana(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
		List goodsList=new ArrayList();
		String sql="select * from t_goods where del='no' order by fangyingshi desc";
		Object[] params={};
		DB mydb=new DB();
		try
		{
			mydb.doPstm(sql, params);
	
	public void goodsDel(HttpServletRequest req,HttpServletResponse res)
	{
		String id=req.getParameter("id");
		String sql="update t_goods set del='yes' where id="+id;
		Object[] params={};
		DB mydb=new DB();
		mydb.doPstm(sql, params);
		mydb.closed();
		
		req.setAttribute("msg", "操作成功");
		String targetURL = "/common/msg.jsp";
		dispatch(targetURL, req, res);
	}
	
	
	public void goodsDetailHou(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
        String id=req.getParameter("id");
		
		req.setAttribute("goods", liuService.getGoods(id));
		req.getRequestDispatcher("admin/goods/goodsDetailHou.jsp").forward(req, res);
	}
	
	
	public void goodsAll(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
	{
		List goodsList=new ArrayList();
		//String sql="select * from t_goods where del='no' and fangyingshi >? order by fangyingshi desc";
		String sql="select * from t_goods where del='no' order by fangyingshi desc";
		Object[] params={new SimpleDateFormat("yyyy-MM-dd").format(new Date())};
		DB mydb=new DB();
		try
		{
			mydb.doPstm(sql, null);
			ResultSet rs=mydb.getRs();
			while(rs.next())
			{
				Tgoods goods=new Tgoods();
				
				goods.setId(rs.getString("id"));
				goods.setFangyingshi(rs.getString("fangyingshi"));
				goods.setCatelog_id(rs.getString("catelog_id"));
				
				goods.setMingcheng(rs.getString("mingcheng"));
				goods.setJieshao(rs.getString("jieshao"));
				goods.setFujian(rs.getString("fujian"));
				
				goods.setShichangjia(rs.getInt("shichangjia"));
				goods.setTejia(rs.getInt("tejia"));
				goods.setDel(rs.getString("del"));

请添加图片描述

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

<think>好的,我现在需要解决用户的问题,即基于JavaWebMySQLJSPServlet实现一个简单的购物车系统。首先,我得分析用户的需求,明确购物车的基本功能。通常购物车需要用户登录、浏览商品、添加商品到购物车、查看购物车、修改数量、删除商品以及结算等功能。接下来,我得考虑技术栈的选择。用户已经指定使用JSPServletJavaMySQL,所以需要确保这些技术的整合。首先,数据库设计方面,可能需要用户表、商品表和购物车表。用户表存储用户信息,商品表存储商品详情,购物车表则记录用户选择的商品及其数量。然后,结构设计方面,应该采用MVC模式,Servlet作为控制器,JSP负责视图,模型部分由JavaBean和DAO层处理数据库操作。需要创建相应的包结构,比如com.example.dao、com.example.model、com.example.servlet等。在实现步骤上,首先需要搭建数据库,创建三个表:user、product、cart。用户表包括id、用户名和密码;商品表有id、名称、价格和库存;购物车表则包含用户ID、商品ID和数量。然后,编写实体类对应这些表的结构,并创建DAO类来处理数据库的增删改查操作,比如UserDAO、ProductDAO和CartDAO。接下来是Servlet的编写。需要处理用户登录、商品列表展示、添加购物车、查看购物车、修改数量、删除商品和结算等操作。每个Servlet负责不同的功能,比如LoginServlet验证用户信息,AddToCartServlet处理添加商品到购物车,ViewCartServlet获取购物车信息并展示。在JSP页面部分,需要登录页面login.jsp,商品列表页product_list.jsp,购物车页面cart.jsp。这些页面通过表单和链接与Servlet交互,比如在商品列表页点击“加入购物车”会触发AddToCartServlet,传递商品ID和数量。 此外,需要考虑会话管理,使用HttpSession来保持用户登录状态。在用户登录成功后,将用户对象存入session,后续操作中验证用户是否已登录,否则跳转回登录页面。在测试阶段,需要确保各个功能模块正常工作,比如用户登录是否正确验证,添加商品到购物车是否更新数据库,购物车页面是否能正确显示所选商品及总价,结算功能是否清空购物车并减少库存。可能遇到的问题包括数据库连接失败、Servlet配置错误、JSP页面数据展示不正确等。需要检查web.xml中的Servlet配置,确保URL映射正确,以及DAO层中的SQL语句是否正确,特别是购物车表的操作,可能需要联表查询商品信息。 最后,用户可能希望扩展功能,比如订单管理、商品分类、分页显示等,但当前问题只需实现基本功能。因此,在回答时需要明确核心功能的实现步骤,并提供示例代码,同时指出可能的扩展方向。</think>以下是基于JavaWeb+MySQLJSP+Servlet购物车实现方案: 1. 数据库设计(MySQL) ```sql -- 用户表 CREATE TABLE user ( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50) NOT NULL, password VARCHAR(50) NOT NULL ); -- 商品表 CREATE TABLE product ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) NOT NULL, price DECIMAL(10,2) NOT NULL, stock INT NOT NULL ); -- 购物车表 CREATE TABLE cart ( user_id INT, product_id INT, quantity INT, PRIMARY KEY (user_id, product_id), FOREIGN KEY (user_id) REFERENCES user(id), FOREIGN KEY (product_id) REFERENCES product(id) ); ``` 2. 项目结构 ``` src/ ├── main/ │ ├── java/ │ │ ├── com/example/dao/ (数据库操作类) │ │ ├── com/example/model/ (实体类) │ │ └── com/example/servlet/ (Servlet) │ ├── webapp/ │ ├── WEB-INF/ │ ├── css/ │ ├── jsp/ (视图页面) │ └── lib/ (依赖库) ``` 3. 核心代码示例 实体类(Product.java): ```java public class Product { private int id; private String name; private BigDecimal price; private int stock; // 构造方法/getter/setter } ``` Servlet示例(AddToCartServlet.java): ```java @WebServlet("/addToCart") public class AddToCartServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); User user = (User) session.getAttribute("user"); if (user == null) { response.sendRedirect("login.jsp"); return; } int productId = Integer.parseInt(request.getParameter("productId")); int quantity = Integer.parseInt(request.getParameter("quantity")); CartDAO cartDAO = new CartDAO(); try { cartDAO.addToCart(user.getId(), productId, quantity); response.sendRedirect("productList"); } catch (SQLException e) { throw new ServletException(e); } } } ``` JSP示例(cart.jsp): ```jsp <%@ page import="com.example.model.*,java.util.List" %> <% List<CartItem> cartItems = (List<CartItem>) request.getAttribute("cartItems"); %> <table> <tr> <th>商品名称</th> <th>单价</th> <th>数量</th> <th>小计</th> <th>操作</th> </tr> <% for (CartItem item : cartItems) { %> <tr> <td><%= item.getProduct().getName() %></td> <td>¥<%= item.getProduct().getPrice() %></td> <td> <form action="updateCart" method="post"> <input type="number" name="quantity" value="<%= item.getQuantity() %>"> <input type="hidden" name="productId" value="<%= item.getProduct().getId() %>"> <button type="submit">更新</button> </form> </td> <td>¥<%= item.getProduct().getPrice().multiply(new BigDecimal(item.getQuantity())) %></td> <td><a href="removeFromCart?productId=<%= item.getProduct().getId() %>">删除</a></td> </tr> <% } %> </table> ``` 实现说明: 1. 使用JDBC进行数据库操作 2. 采用MVC模式:Servlet作为控制器,JSP作为视图,JavaBean作为模型 3. 使用Session保持用户登录状态 4. 购物车数据持久化存储在数据库 5. 包含基本的商品浏览、加入购物车、修改数量、删除商品等功能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值