Javaweb 图书馆程序设计介绍

目录结构

Javaweb 图书馆程序设计介绍_第1张图片
Javaweb 图书馆程序设计介绍_第2张图片

index.jsp 书城首页 遍历书籍 web界面

Javaweb 图书馆程序设计介绍_第3张图片

<c:forEach items="${page.list }" var="menu">
															<div style="margin-top: 10px; margin-left: 30px; float: left; width: 300px" >
																<table  >
																	<tr >
																		<td rowspan="5" class="bookPic"><img
																			src="${menu.imgpath}" width="150px" height="140px" style="border-radius:10px"
																			 /></td>
																		<td><span>书名:</span></td>
																		<td><span><strong>${menu.name}</strong></span></td>
																	</tr>
																	<tr>
																		<td><span>市场价格:</span></td>
																		<td><span>${menu.price}</span></td>
																	</tr>
																	<tr>
																		<td><span>会员价格:</span></td>
																		<td><span><strong style="color: red;">${menu.price1}</strong></span></td>
																	</tr>
																	<tr>
																		<td><span>标签:</span></td>
																		<td><span>${menu.burden}</span></td>
																	</tr>
																	<tr>
																		<td><span>书籍类型:</span></td>
																		<td><span>${menu.typename}</span></td>
																	</tr>
																	<tr>
																		<td colspan="2" style="height: 40px;"><a href="${pageContext.request.contextPath}/shoppingCartServlet?action=add&&menuid=${menu.id}&&curPage=${page.curPage}"><img src=""><img src="${pageContext.request.contextPath}/qiantai/images/3.jpg" width=50 height=28 title="加入购物车"><strong>购物车</strong></a></td>
																		<td></td>
																	</tr>
																</table>
															</div>
														</c:forEach>

MenusServlet.java 查询所有书籍

private void findMenusAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//获取当前页码
		String curPage=request.getParameter("curPage");
		int curpagestr=0;
		if(curPage==null||"".equals(curPage)){
			curpagestr=1;
		}else{
			curpagestr=Integer.parseInt(curPage);
		}
		//把当前页码赋值给Page的curpage属性
		Page page=new Page();
		page.setCurPage(curpagestr);
		//调用Service分页查询方法
		page=menusService.getMenusAll(page);
		//request绑定查询结果
		request.setAttribute("page", page);
		//请求转发
		request.getRequestDispatcher("/admin/menus.jsp").forward(request, response);
	}

MenusDaoImpl.java 数据库查询

@Override
	public List<Menus> getMenusAll() {
		String sql="select menus.id, menus.name,imgpath,burden,types.name as typename,brief,price1,sums1,price,sums from menus inner JOIN types on types.id=menus.typeid ORDER BY sums1 desc LIMIT 0,2 ";
		List list=null;
		try {
			list=dbutil.getQueryList(Menus.class, sql, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

Javaweb 图书馆程序设计介绍_第4张图片

index.jsp 公告遍历:

<c:forEach items="${requestScope.notice }" var="notice" begin="0" end="1">

															<ul class="newslist_time2">
																
																
																	
																	<li class="newslist_time2">
																		<div class="time">${notice.times.substring(0,10) }</div>
																		<a href="${pageContext.request.contextPath }/indexServlet?action=findNotice&&id=${notice.id }" class="newslist_time2">${notice.name }</a>
																	</li>
																
																
															</ul>
															</c:forEach>

NoticeServlet.java 查询公告

private void findAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		List<Notice> list=noticeService.getNoticeAll();
		
		//2.将查询结果保存在request域对象中
		request.setAttribute("notices",list);
		request.getRequestDispatcher("/admin/notice.jsp").forward(request, response);
	}

删除公告

private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		PrintWriter out=response.getWriter();
		
		int id=Integer.parseInt(request.getParameter("id"));
		Notice notice=new Notice();
		notice.setId(id);
		int result=noticeService.deleteNotice(id);
		if(result==1){
			out.write("");
		}else if(result==0){
			out.write("");
		}
		
	}

添加公告

private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		PrintWriter out=response.getWriter();
		String name=request.getParameter("name");
		String content=request.getParameter("content");
		//给types实体类的name赋值
		System.out.println(name);
		Notice notice=new Notice();
		notice.setName(name);
		notice.setContent(content);
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time=df.format(new Date());
		notice.setTimes(time);
		//调用添加方法
		int result=noticeService.addNotice(notice);
		if(result==1){
			out.write("");
		}else if(result==-1){
			out.write("");
		}else{
			out.write("");
		}
		//判读是否成功
	}

index.jsp 销售量遍历

Javaweb 图书馆程序设计介绍_第5张图片

<c:forEach items="${sessionScope.shoppingCart }" var="cart">
																	<tr>
																		<td align="center">${cart.name}</td>
																		<td align="center">${cart.price}</td>
																		<td align="center">${cart.sum}</td>
																		<td align="center"><a
																			href="${pageContext.request.contextPath}/shoppingCartServlet?action=del&&menuid=${cart.menuid}&&curPage=${page.curPage}">取消</a></td>
																			<c:set var="count" value="${count+cart.sum }"></c:set>
																			<c:set var="price1" value="${price1+cart.sum*cart.price }"></c:set>
																	</tr>
																	</c:forEach>

OrderDaoImpl.java 数据库查询订单

@Override
	public Page getOrderAll(Page page) {
		// TODO Auto-generated method stub
		String sql="select menus.id menuid ,menus.name menuname,SUM(menusum) sum,menus.price price from menus inner join orders on menus.id=orders.menuid   group by menuid order by sum desc ";
		Page page1=null;
		
			page1 = dbutil.getQueryPage(OrderStatistics.class, sql, null, page);
		
		return page1;
	}

Shoppingcar.jsp 购物车web界面

<tr>
											<td class="line_table" align="center" width="40%"><span
												class="left_bt2">菜品名称</span></td>
											<td class="line_table" align="center" width="20%"><span
												class="left_bt2">单价</span></td>
											<td class="line_table" align="center" width="20%"><span
												class="left_bt2">数量</span></td>
											<td class="line_table" align="center" width="20%"><span
												class="left_bt2">&nbsp;&nbsp;</span></td>
										</tr>
                                        <c:forEach items="${sessionScope.shoppingCart}" var="cart">

										<tr>
											<td class="line_table" align="center" width="40%"><span
												class="left_txt">${cart.name}</span></td>
											<td class="line_table" align="center" width="20%"><span
												class="left_txt">${cart.price}</span></td>
											<td class="line_table" align="center" width="20%"><span
												class="left_txt">${cart.sum}</span></td>
											<td class="line_table" align="center" width="20%"><a
								                href="${pageContext.request.contextPath }/shoppingCartServlet?action=del&&menuid=${cart.menuid}">取消</a></td>
										<c:set var="count" value="${count+cart.sum}"> </c:set>
			                          	<c:set var="price1" value="${price1+cart.sum*cart.price}"></c:set>
										
										</tr>
										</c:forEach>

ShoppingCarServlet.java 购物车

Javaweb 图书馆程序设计介绍_第6张图片

添加购物车

private void addCart(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		int menuid=Integer.parseInt(request.getParameter("menuid"));
		//获取购物车
		HttpSession session=request.getSession();
		//判断是否有该购物车
		ArrayList<ShoppingCart> cartList=(ArrayList<ShoppingCart>) session.getAttribute("shoppingCart");
		boolean flag=false;
		//如果没有,创建新的购物车
		if(cartList==null){
			//创建一个购物车
			cartList=new ArrayList<ShoppingCart>();
			
		}else{
			for(ShoppingCart cart:cartList){
				if(cart.getMenuid()==menuid){
					//数量加一
					cart.setSum(cart.getSum()+1);
					flag=true;
					break;
				}
			}
		}
		//如果有,数量加一
		
		//如果没有购物车,创建购物车实例
		if(!flag)
		{
			ShoppingCart cart=new ShoppingCart();
			//根据菜品ID查询
			Menus menus=service.getMenusByID(menuid);
			//添加菜品id
			cart.setMenuid(menuid);
			//菜品名称
			cart.setName(menus.getName());
			//单价
			cart.setPrice(Float.parseFloat(menus.getPrice()));
			cart.setSum(1);
			//把购物车信息放到list中
			cartList.add(cart);
			
		}
		//购物车信息加入session
		session.setAttribute("shoppingCart", cartList);
		//请求重定向
		response.sendRedirect(request.getContextPath()+"/qiantai/index.jsp");
		
	}

在该页面中还有查询购物车,清空购物车

Javaweb 图书馆程序设计介绍_第7张图片

order.jsp 前台订单界面

<c:forEach items="${requestScope.page.list }" var="order">
										<tr  height=30>
											<td class="line_table" align="center"><span
												class="left_txt"><strong>${order.menuname }</span></td>
											<td class="line_table" align="center"><span
												class="left_txt"><strong>${order.realname }</span></td>
											<td class="line_table" align="center"><span
												class="left_txt"><strong>${order.phone }</span></td>
											<td class="line_table" align="center"><span
												class="left_txt"><strong>${order.address }</span></td>
											<td class="line_table" align="center"><span
												class="left_txt"><strong>${order.menusum}</span></td>
											<td class="line_table" align="center"><span
												class="left_txt"><strong>${order.price}</span></td>
											<td class="line_table" align="center"><span
												class="left_txt"><strong><font color=red>${order.price*order.menusum }</span></td>
											<td class="line_table" align="center"><span
												class="left_txt"><strong>${order.times}</span></td>
											<td class="line_table" align="center"><span
												class="left_txt"><strong>${order.delivery}</strong></span></td>
												
										</tr>
										<tr><th colspan=9 height=2 bgcolor="#b8b2a6"></th></tr>
										</c:forEach>

OrderServlet.java 前台订单

生成订单

private void addOrder(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		PrintWriter out=response.getWriter();
		HttpSession session=request.getSession();
		
		User user=(User) session.getAttribute("user");
		if(user!=null){
			//用户登陆后才能获取购物车
			ArrayList<ShoppingCart> cartList=(ArrayList<ShoppingCart>) session.getAttribute("shoppingCart");
			boolean result;
			try {
				result=orderService.addOrders(cartList, user.getId());
				if(result){
					//此时已将购物车加入订单
					session.removeAttribute("shoppingCart");
					out.write("");
					
				}else{
					out.write("");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			out.write("");
		}
		
	}

}

插入订单数据库

public int addOrders(Orders orders) {
		// TODO Auto-generated method stub
		String sql="insert into orders(userid,menuid,menusum,times,delivery) values(?,?,?,?,?)";
		Object[] obj={orders.getId(),orders.getMenuid(),orders.getMenusum(),orders.getTimes(),orders.getDelivery()};
		int result=0;
		try {
			result=dbutil.execute(DBUtil.getConnection(), sql, obj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}

查询该用户订单

private void findOrdersAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
			//获取当前页码
			String curPage=request.getParameter("curPage");
			int curpagestr=0;
			if(curPage==null||"".equals(curPage)){
				curpagestr=1;
			}else{
				curpagestr=Integer.parseInt(curPage);
			}
			//把当前页码赋值给Page的curpage属性
			Page1 page=new Page1();
			page.setCurPage(curpagestr);
			//调用Service分页查询方法
			page=orderService.getOrderAll1(page);
			//request绑定查询结果
			request.setAttribute("page", page);
			//请求转发
			request.getRequestDispatcher("/admin/order.jsp").forward(request, response);
	}

数据库操作:根据用户名,用户ID,书籍名称

public Page getOrderByID(Page page, String userid) {
		// TODO Auto-generated method stub
		String sql="select userid,users.realname,users.phone,users.address,menus.name menuname,orders.menusum,menus.price,orders.times,delivery from orders inner join users on orders.userid=users.id inner join menus on orders.menuid=menus.id where users.name=? order by orders.times desc";
		Page page1=null;
		Object[] obj={userid};
		
			//page1 = dbutil.getQueryPage(Orders.class, sql, null, page);
			page1=dbutil.getQueryPage(Orders.class, sql, obj, page);
		
		return page1;
	}
	@Override
	public Page getOrderByMenuName(Page page, String userid, String menuname) {
		// TODO Auto-generated method stub
		String sql="select users.realname,users.phone,users.address,menus.name menuname,orders.menusum,menus.price,orders.times,delivery from orders inner join users on orders.userid=users.id inner join menus on orders.menuid=menus.id where users.name=? and menus.name like '%"+menuname+"%'";
		Page page1=null;
		Object[] obj={userid};
		
			//page1 = dbutil.getQueryPage(Orders.class, sql, null, page);
			page1=dbutil.getQueryPage(Orders.class, sql, obj, page);
		
		return page1;
	}
	@Override
	public Page getadminOrderByMenuName(Page page,  String menuname) {
		// TODO Auto-generated method stub
		String sql="select  users.realname,users.phone,users.address,menus.name menuname,orders.menusum,menus.price,orders.times,delivery from orders inner join users on orders.userid=users.id inner join menus on orders.menuid=menus.id where menus.name like '%"+menuname+"%'";
		Page page1=null;
		
		
			//page1 = dbutil.getQueryPage(Orders.class, sql, null, page);
			page1=dbutil.getQueryPage(Orders.class, sql, null, page);
		
		return page1;
	}

其余前端界面代码不做赘述

下面开始介绍后台代码

Javaweb 图书馆程序设计介绍_第8张图片

书籍列表

Javaweb 图书馆程序设计介绍_第9张图片

<c:forEach items="${requestScope.page.list }" var="menus">
							<tr onmouseover="this.style.backgroundColor='#c5c5c5';" onmouseout="this.style.backgroundColor='#ececec';">
								<td class="line_table" align="center"><a
									href="menus_update.jsp?">${menus.name}</a></td>
								<td class="line_table" align="center" height=55 width=65><a
									href="${pageContext.request.contextPath }/${menus.imgpath}"><img src="${pageContext.request.contextPath }/${menus.imgpath}"
										width="60" height="50"></a></td>
								<td class="line_table" align="center"><span
									class="left_txt">${menus.burden}</span></td>
								<td class="line_table" align="center"><span
									class="left_txt">${menus.typename}</span></td>
								<td class="line_table" align="center"><span
									class="left_txt">${menus.brief}</span></td>
								<td class="line_table" align="center"><span
									class="left_txt">${menus.price}</span></td>
								<td class="line_table" align="center"><span
									class="left_txt">${menus.sums}</span></td>
								<td class="line_table" align="center"><span
									class="left_txt">${menus.price1}</span></td>
								<td class="line_table" align="center"><span
									class="left_txt">${menus.sums1}</span></td>
								<td class="line_table" align="center"><a
									href="${pageContext.request.contextPath}/menusServlet?action=findID&&id=${menus.id}"><img src="${pageContext.request.contextPath}/admin/images/update1.png" width=80 height=35 style="border-radius:5px" title="修改"></a></td>
								<td class="line_table" align="center"><a
									href="${pageContext.request.contextPath}/menusServlet?action=delete&&name=${menus.name}"><img src="${pageContext.request.contextPath}/admin/images/delete.png" width=80 height=33 style="border-radius:5px" title="删除"></a></td>
							</tr>
							<tr><th colspan=11 height=8></th></tr>
							</c:forEach>

根据书籍名称。用户名称查询 order_search.jsp

<form action="${pageContext.request.contextPath}/ordersServlet?action=orderSearch4&&curPage=1" name="form1"
						method="post">
						
						 		<tr>
									<td class="line_table" align="right" width="40%"><span
										class="left_bt2">按用户姓名查询</span></td>
									<td class="line_table" align="left" width="60%"><input
										type="text" name="username" size="20"> 
										</td><tr>
								<td class="line_table" align="right" width="40%"><span
													class="left_bt2">按菜品名称查询</span></td>
													
												<td class="line_table" align="left" width="60%"><input
													type="text" name="menuname" size="20">
								<input
										type="submit" value="." style="background-image:url(${pageContext.request.contextPath}/admin/images/find.png); background-size:100%;opacity:0.8;background-color:#5ca0d3;height:25px;width:70px;font-size:20px;border:none;border-radius:10px"></td>	
								
						</table>
					</form>

其余功能较为相似,不做赘述

你可能感兴趣的:(酷酷的猿,javaweb,毕业设计)