最简单的小型商城实现:Serlvet+Jsp经典案例

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ruiguang21/article/details/53728046

1. 前台client有关的页面

【header.jsp】

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"  
  2.       contentType="text/html;charset=UTF-8"%>  
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  4. <%@ taglib uri="http://java.sun.com/jsp/jstl/functions"  prefix="fn"%>  
  5. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  6. <html>  
  7.   <head>    
  8.     <link href="${pageContext.request.contextPath}/css/css.css" rel="stylesheet" type="text/css" />    
  9.     <title>首页</title>     
  10.   </head>  
  11.     
  12. <body style="text-align:center">  
  13. <br />  
  14.     <a href="${pageContext.request.contextPath}">首页</a>  
  15.     <span style="width:800px"></span>  
  16.     <c:if test="${empty user}"><!-- 也可以是:${sessionScope.user==null} -->  
  17.         <a href="${pageContext.request.contextPath}/client/login.jsp">登录</a>  
  18.         <a href="${pageContext.request.contextPath}/client/regist.jsp">注册</a>  
  19.     </c:if>  
  20.     <c:if test="${!empty user}">  
  21.         欢迎您:${user.username}  <!-- ${sessionScope.user.username} -->  
  22.         <a href="${pageContext.request.contextPath}/clientServlet?operation=logout">注销</a>  
  23.     </c:if>  
  24.     <a href="${pageContext.request.contextPath}/clientServlet?operation=showUsersOrders">我的订单</a>    
  25.     <a href="${pageContext.request.contextPath}/client/showCart.jsp">我的购物车</a>  
  26. <br />  
  27. <script language="javascript">  
  28.     function qiehuan(num){  
  29.         for(var id = 0;id<=9;id++)  
  30.         {  
  31.             if(id==num)  
  32.             {  
  33.                 document.getElementById("qh_con"+id).style.display="block";  
  34.                 document.getElementById("mynav"+id).className="nav_on";  
  35.             }  
  36.             else  
  37.             {  
  38.                 document.getElementById("qh_con"+id).style.display="none";  
  39.                 document.getElementById("mynav"+id).className="";  
  40.             }  
  41.         }  
  42.     }  
  43. </script>  
  44. <div id=menu_out>  
  45.   <div id=menu_in>  
  46.     <div id=menu>  
  47.       <UL id=nav>  
  48.         <LI><A class=nav_on id=mynav0 onmouseover=javascript:qiehuan(0) href="#"><SPAN>所有分类</SPAN></A></LI>  
  49.         <LI class="menu_line"></LI>  
  50.     </UL>  
  51.       <div id=menu_con>  
  52.         <div id=qh_con0 style="DISPLAY: block">  
  53.           <UL>  
  54.           <c:forEach items="${sessionScope.cs}" var="c">  
  55.                 <LI><A href="${pageContext.request.contextPath}/clientServlet?operation=showCategoryBook&categoryId=${c.id}"><SPAN>${c.name }</SPAN></A></LI>  
  56.                 <LI class=menu_line2></LI>  
  57.             </c:forEach>  
  58.             </UL>  
  59.          </div>  
  60.       </div>  
  61.     </div>  
  62.   </div>  
  63.   </div>  

【listOrder.jsp】

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"  
  2.       contentType="text/html;charset=UTF-8"%>  
  3. <%@ include file="/client/header.jsp" %>  
  4.     <h1>您近期的订单如下:</h1>  
  5.         <table border="1" width="88%" style="text-align:center" align="center">  
  6.             <tr>  
  7.                 <th>订单号</th>  
  8.                 <th>金额</th>  
  9.                 <th>订单状态</th>  
  10.                 <th>明细</th>  
  11.               
  12.             </tr>  
  13.             <c:forEach items="${ os}" var="o">  
  14.                 <tr>  
  15.                     <td>${o.ordernum}</td>  
  16.                     <td>¥${o.price }元</td>  
  17.                     <td>${o.state==0?'未发货':'已发货'}</td>  
  18.                     <td>  
  19.                         <a href="${pageContext.request.contextPath}/clientServlet?operation=showOrdersDetail&ordersId=${o.id}">订单详情</a>  
  20.                     </td>  
  21.                 </tr>  
  22.             </c:forEach>  
  23.         </table>  
  24.   </body>  
  25. </html>  


【login.jsp】

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"  
  2.       contentType="text/html;charset=UTF-8"%>  
  3. <%@ include file="/client/header.jsp" %>  
  4.     <h1>用户登录</h1>     
  5.     <form action="${pageContext.request.contextPath}/clientServlet?operation=login" method="post">   
  6.         <table border="1" width="88%" style="text-align:center" align="center">  
  7.             <tr>  
  8.                 <td>用户名</td>  
  9.                 <td>  
  10.                     <input type="text" name="username"/>  
  11.                 </td>  
  12.             </tr>  
  13.             <tr>  
  14.                 <td>密码</td>  
  15.                 <td>  
  16.                     <input type="password" name="password"/>  
  17.                 </td>  
  18.             </tr>  
  19.             <tr>  
  20.                 <td colspan="2">  
  21.                     <input type="submit" value="登录"/>  
  22.                 </td>  
  23.             </tr>  
  24.               
  25.         </table>  
  26.     </form>  
  27.   </body>  
  28. </html>  


【orderMsg.jsp】

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"  
  2.       contentType="text/html;charset=UTF-8"%>  
  3. <%@ include file="/client/header.jsp" %>  
  4.     订单号:${o.ordernum}  </br>金额:¥${o.price }元</br>  
  5.     明细如下:</br>  
  6.         <table border="1" width="88%" style="text-align:center" align="center">  
  7.             <tr>  
  8.                 <th>书名</th>  
  9.                 <th>作者</th>  
  10.                 <th>单价</th>  
  11.                 <th>小计</th>  
  12.                 <th>数量</th>  
  13.             </tr>  
  14.             <c:forEach items="${o.items }" var="i">  
  15.                 <tr>  
  16.                     <td>${i.book.name}</td>  
  17.                     <td>${i.book.author}</td>  
  18.                     <td>¥${i.book.price}元</td>  
  19.                     <td>${i.num}</td>  
  20.                     <td>¥${i.price}元</td>  
  21.                 </tr>  
  22.             </c:forEach>  
  23.         </table>  
  24.     收货信息如下:</br>  
  25.         地址:${sessionScope.user.address}</br>        
  26.         电话:${sessionScope.user.cellphone}</br>  
  27.   </body>  
  28. </html>  

【regist.jsp】

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"  
  2.       contentType="text/html;charset=UTF-8"%>  
  3. <%@ include file="/client/header.jsp" %>  
  4.     <h1>新用户注册</h1>    
  5.     <form action="${pageContext.request.contextPath}/clientServlet?operation=regist" method="post">   
  6.         <table border="1" width="88%" style="text-align:center" align="center">  
  7.             <tr>  
  8.                 <td>用户名</td>  
  9.                 <td>  
  10.                     <input type="text" name="username" />  
  11.                 </td>  
  12.             </tr>  
  13.             <tr>  
  14.                 <td>密码</td>  
  15.                 <td>  
  16.                     <input type="password" name="password" />  
  17.                 </td>  
  18.             </tr>  
  19.             <tr>  
  20.                 <td>确认密码</td>  
  21.                 <td>  
  22.                     <input type="password" name="password1" />  
  23.                 </td>  
  24.             </tr>  
  25.             <tr>  
  26.                 <td>电话</td>  
  27.                 <td>  
  28.                     <input type="text" name="cellphone" />  
  29.                 </td>  
  30.             </tr>  
  31.             <tr>  
  32.                 <td>手机</td>  
  33.                 <td>  
  34.                     <input type="text" name="mobilephone" />  
  35.                 </td>  
  36.             </tr>  
  37.             <tr>  
  38.                 <td>住址</td>  
  39.                 <td>  
  40.                     <input type="text" name="address" />  
  41.                 </td>  
  42.             </tr>  
  43.             <tr>  
  44.                 <td>邮箱</td>  
  45.                 <td>  
  46.                     <input type="text" name="email" />  
  47.                 </td>  
  48.             </tr>  
  49.             <tr>  
  50.                 <td colspan="2">  
  51.                     <input type="submit" value="注册" />  
  52.                 </td>  
  53.             </tr>  
  54.         </table>    
  55.     </form>  
  56.   </body>  
  57. </html>  


【showChart.jsp】


[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"  
  2.       contentType="text/html;charset=UTF-8"%>  
  3. <%@ include file="/client/header.jsp" %>  
  4.     <h1>您购买的商品如下</h1>  
  5.     <c:if test="${empty sessionScope.cart.items}">  
  6.         <img height="180" width="120" src="${pageContext.request.contextPath }/files/0.jpg"/><br/>对不起,您没有购买任何商品  
  7.     </c:if>  
  8.     <c:if test="${!empty sessionScope.cart.items}">  
  9.         <table border="1" width="88%" style="text-align:center" align="center">  
  10.             <tr>  
  11.                 <th>书名</th>  
  12.                 <th>作者</th>  
  13.                 <th>单价</th>  
  14.                 <th>数量</th>  
  15.                 <th>小计</th>  
  16.                 <th>操作</th>  
  17.             </tr>  
  18.             <c:forEach items="${ sessionScope.cart.items}" var="me">  
  19.                 <tr>  
  20.                     <td>${me.value.book.name}</td>  
  21.                     <td>${me.value.book.author }</td>  
  22.                     <td>${me.value.book.price }</td>  
  23.                     <td>${me.value.num }</td>  
  24.                     <td>${me.value.price}</td>  
  25.                     <td>  
  26.                         <a href="#" >删除</a>  
  27.                     </td>  
  28.                 </tr>  
  29.             </c:forEach>  
  30.             <tr>  
  31.                 <td colspan="6" align="right">  
  32.                     总数量:${sessionScope.cart.num}    
  33.                     付款金额:${sessionScope.cart.price}    
  34.                     <a href="${pageContext.request.contextPath }/clientServlet?operation=genOrders">生成订单</a>  
  35.                 </td>  
  36.             </tr>  
  37.         </table>  
  38.     </c:if>  
  39.   </body>  
  40. </html>  

【welcome.jsp】

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"  
  2.       contentType="text/html;charset=UTF-8"%>  
  3. <%@ include file="/client/header.jsp" %>  
  4. <h1>欢迎光临</h1>  
  5.     <!-- 显示分页数据,不分类的 -->  
  6.     <table align="center">  
  7.         <tr >  
  8.             <c:forEach items="${page.records}" var="b">  
  9.                 <td>  
  10.                     <table>  
  11.                         <tr>  
  12.                             <td>  
  13.                                 <img src="${pageContext.request.contextPath}/files/${b.image}"/>  
  14.                             </td>  
  15.                         </tr>  
  16.                         <tr>  
  17.                             <td>  
  18.                                 书名:${b.name }<br />  
  19.                                 作者:${b.author }<br />  
  20.                                 原价:<strike>888</strike><br/>  
  21.                                 震撼价:${b.price}  
  22.                             </td>  
  23.                         </tr>  
  24.                         <tr>  
  25.                             <td>  
  26.                                 <a href="${pageContext.request.contextPath}/clientServlet?operation=buyBook&bookId=${b.id}">购买</a>  
  27.                             </td>  
  28.                         </tr>  
  29.                     </table>  
  30.                 </td>  
  31.             </c:forEach>  
  32.         </tr>  
  33.     </table>  
  34.               
  35.         <tr align="center">  
  36.             <td>  
  37.                 <%@ include file="/commons/page.jsp" %>  
  38.             </td>  
  39.         </tr>  
  40.       
  41.   </body>  
  42. </html>  

2. 前台manager相关页面

【header.jsp】

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"  
  2.       contentType="text/html;charset=UTF-8"%>  
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>  
  4. <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>  
  5. <%@ taglib uri="http://hw.com/jsp/jstl/myfunctions" prefix="myfn" %>  
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  7. <html>  
  8.     <link href="${pageContext.request.contextPath}/css/css.css" rel="stylesheet" type="text/css" />  
  9.   <head>    
  10.     <title>首页</title>     
  11.   </head>  
  12.     
  13.   <body style="text-align:center">  
  14.     
  15.   <br />  
  16. <br />  
  17. <script language="javascript">  
  18.     function qiehuan(num){  
  19.         for(var id = 0;id<=9;id++)  
  20.         {  
  21.             if(id==num)  
  22.             {  
  23.                 document.getElementById("qh_con"+id).style.display="block";  
  24.                 document.getElementById("mynav"+id).className="nav_on";  
  25.             }  
  26.             else  
  27.             {  
  28.                 document.getElementById("qh_con"+id).style.display="none";  
  29.                 document.getElementById("mynav"+id).className="";  
  30.             }  
  31.         }  
  32.     }  
  33. </script>  
  34. <div id=menu_out>  
  35.   <div id=menu_in>  
  36.     <div id=menu>  
  37.       <UL id=nav>  
  38.         <LI><A class=nav_on id=mynav0 onmouseover=javascript:qiehuan(0) href="#"><SPAN>分类管理</SPAN></A></LI>  
  39.         <LI class="menu_line"></LI>  
  40.         <li><a href="#" onmouseover="javascript:qiehuan(1)" id="mynav1" class="nav_off"><span>图书管理</span></a></li>  
  41.         <li class="menu_line"></li>  
  42.         <li><a href="#" onmouseover="javascript:qiehuan( 2)" id="mynav2" class="nav_off"><span>订单管理</span></a></li>  
  43.         <li class="menu_line"></li>  
  44.           
  45.           
  46.       </UL>  
  47.       <div id=menu_con>  
  48.         <div id=qh_con0 style="DISPLAY: block">  
  49.           <UL>  
  50.             <LI><a href="${pageContext.request.contextPath}/manage/addCategory.jsp"><span>添加分类</span></A></LI>  
  51.             <LI class=menu_line2></LI>  
  52.             <LI><A href="${pageContext.request.contextPath}/manageServlet?operation=listCategory"><SPAN>查询分类</SPAN></A></LI>  
  53.             <LI class=menu_line2></LI>  
  54.              
  55.           </UL>  
  56.         </div>  
  57.         <div id=qh_con1 style="DISPLAY: none">  
  58.           <UL>  
  59.             <LI><a href="${pageContext.request.contextPath}/manageServlet?operation=showCategoryUI"><span>添加图书</span></A></LI>  
  60.             <LI class=menu_line2></LI>  
  61.             <LI><A href="${pageContext.request.contextPath}/manageServlet?operation=showAllBook"><SPAN>查询图书</SPAN></A></LI>  
  62.             <LI class=menu_line2></LI>  
  63.              
  64.           </UL>  
  65.         </div>  
  66.         <div id=qh_con2 style="DISPLAY: none">  
  67.           <UL>  
  68.             <LI><a href="${pageContext.request.contextPath}/manageServlet?operation=showAllOrders0"><span>待处理订单</span></A></LI>  
  69.             <LI class=menu_line2></LI>  
  70.             <LI><A href="${pageContext.request.contextPath}/manageServlet?operation=showAllOrders1"><SPAN>已处理订单</SPAN></A></LI>  
  71.             <LI class=menu_line2></LI>  
  72.           </UL>  
  73.         </div>    
  74.       </div>  
  75.     </div>  
  76.   </div>  
  77. </div>  


【listBook.jsp】

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"  
  2.       contentType="text/html;charset=UTF-8"%>  
  3. <%@ include file="/manage/header.jsp" %>  
  4. <h3>书籍列表</h3>  
  5.     <table border="1" width="88%" align="center" style="text-align:center">  
  6.         <tr>  
  7.             <th>书名</th>  
  8.             <th>作者</th>  
  9.             <th>描述</th>  
  10.             <th>所属分类</th>  
  11.             <th>图片</th>  
  12.             <th>操作</th>  
  13.         </tr>  
  14.         <c:forEach items="${page.records}" var="b">  
  15.             <tr>  
  16.                 <td>${ b.name}</td>  
  17.                 <td>${b.author }</td>  
  18.                 <td>${b.description }</td>  
  19.                 <td>${myfn:getCategoryNameByIdEL(b.category_id)}</td>  
  20.                 <td>  
  21.                     <a href="${pageContext.request.contextPath}/files/${b.image}">查看图片</a>  
  22.                 </td>  
  23.                 <td>  
  24.                     <a href="#" >[修改]</a>    
  25.                     <a href="#" >[删除]</a>  
  26.                 </td>  
  27.             </tr>  
  28.         </c:forEach>  
  29.     </table>  
  30.     <!-- 视频内容简单分页内容开始 -->  
  31.     <!--    
  32.     第${page.pagenum}页/共${page.totalpage}页    
  33.     <a href="${pageContext.request.contextPath}/manageServlet?operation=showAllBook&pagenum=  
  34.         ${page.pagenum-1>0?pagenum-1:1}">上一页</a>  
  35.     <a href="${pageContext.request.contextPath}/manageServlet?operation=showAllBook&pagenum=  
  36.         ${page.pagenum+1>page.totalpage?page.totalpage:(page.pagenum+1)}">下一页</a>  
  37.         -->  
  38.     <%@ include file="/commons/page.jsp" %>  
  39.           
  40.     <!-- 视频内容简单分页内容结束 -->  
  41.   </body>  
  42. </html>  


【listCategory.jsp】

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"  
  2.       contentType="text/html;charset=UTF-8"%>  
  3. <%@ include file="/manage/header.jsp" %>  
  4.     <h2>分类列表</h2>  
  5.     <c:if test="${empty cs}">  
  6.         对不起,没有分类,请先添加  
  7.     </c:if>  
  8.     <c:if test="${!empty cs}">  
  9.     <table border="1" width="88%" style="text-align:center" align="center">  
  10.         <tr>  
  11.             <th>选择</th>  
  12.             <th>分类名称</th>  
  13.             <th>描述</th>  
  14.             <th>操作</th>  
  15.         </tr>  
  16.         <c:forEach items="${cs}" var="c">  
  17.             <tr>  
  18.                 <td>  
  19.                     <input type="checkbox" name="cids" value="${c.id }"/>  
  20.                 </td>  
  21.                 <td>${c.name }</td>  
  22.                 <td>${c.description }</td>  
  23.                 <td>  
  24.                     <a href="#" >更改</a>  
  25.                     <a href="#" >删除</a>  
  26.                 </td>  
  27.             <tr>  
  28.         </c:forEach>  
  29.     </table>  
  30.     </c:if>  
  31.   </body>  
  32. </html>  


【addBook.jsp】

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"  
  2.       contentType="text/html;charset=UTF-8"%>  
  3. <%@ include file="/manage/header.jsp" %>  
  4.     <h2 align="center">添加图书</h2>  
  5.     <form action="${pageContext.request.contextPath}/manageServlet?operation=addBook" method="post" enctype="multipart/form-data" >  
  6.         <table border="0" width="88%" align="center">  
  7.             <tr>  
  8.                 <td>书名</td>  
  9.                 <td>  
  10.                     <input type="text" name="name"/>  
  11.                 </td>  
  12.             </tr>  
  13.             <tr>  
  14.                 <td>作者</td>  
  15.                 <td>  
  16.                     <input type="text" name="author"/>  
  17.                 </td>  
  18.             </tr>  
  19.             <tr>  
  20.                 <td>售价</td>  
  21.                 <td>  
  22.                     <input type="text" name="price"/>  
  23.                 </td>  
  24.             </tr>  
  25.             <tr>  
  26.                 <td>图片</td>  
  27.                 <td>  
  28.                     <input type="file" name="img"/> <!-- ★和javabean的名字不一样 -->  
  29.                 </td>  
  30.             </tr>  
  31.             <tr>  
  32.                 <td>描述</td>  
  33.                 <td>  
  34.                     <textarea rows="3" cols="38" name="description"></textarea>  
  35.                 </td>  
  36.             </tr>  
  37.             <tr>  
  38.                 <td>所属分类</td>  
  39.                 <td>  
  40.                     <select name="category_id">  
  41.                         <c:forEach items="${cs}" var="c">  
  42.                             <option value="${c.id}">${c.name}</option>   
  43.                         </c:forEach>  
  44.                     </select>  
  45.                 </td>  
  46.             </tr>  
  47.             <tr>  
  48.                 <td>  
  49.                     <input type="submit" value="添加图书"></td>  
  50.             </tr>  
  51.         </table>  
  52.     </form>  
  53.     ${message}  
  54.   </body>  
  55. </html>  


【addCategory.jsp】

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"  
  2.       contentType="text/html;charset=UTF-8"%>  
  3. <%@ include file="/manage/header.jsp" %>  
  4.     <form action="${pageContext.request.contextPath}/manageServlet?operation=addCategory" method="post">    
  5.         <table align="center"  border="1" width="88%" >  
  6.             <tr>  
  7.                 <td>*分类名称</td>  
  8.                 <td>  
  9.                     <input type="text" name="name"/>  
  10.                 </td>  
  11.             </tr>  
  12.             <tr>  
  13.                 <td>概述</td>  
  14.                 <td>  
  15.                     <textarea rows="3" cols="34" name="description"></textarea>  
  16.                 </td>  
  17.             </tr>  
  18.             <tr>  
  19.                 <td colspan="2">  
  20.                     <input type="submit" value="添加"/>  
  21.                 </td>  
  22.             </tr>  
  23.           
  24.         </table>  
  25.     </form>  
  26.   </body>  
  27. </html>  

【showOrders.jsp】

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"  
  2.       contentType="text/html;charset=UTF-8"%>  
  3. <%@ include file="/client/header.jsp" %>  
  4.     <h1>未发货订单如下:</h1>  
  5.         <table border="1" width="88%" style="text-align:center" align="center">  
  6.             <tr>  
  7.                 <th>用户</th>  
  8.                 <th>订单号</th>  
  9.                 <th>金额</th>  
  10.                 <th>订单状态</th>  
  11.                 <th>明细</th>  
  12.               
  13.             </tr>  
  14.             <c:forEach items="${ os}" var="o">  
  15.                 <tr>  
  16.                     <td>${o.user.username}  
  17.                     <td>${o.ordernum}</td>  
  18.                     <td>¥${o.price }元</td>  
  19.                     <td>${o.state==0?'未发货':'已发货'}</td>  
  20.                     <td>  
  21.                         <a href="${pageContext.request.contextPath}/manageServlet?operation=showOrdersDetail&ordersId=${o.id}">订单详情</a>  
  22.                     </td>  
  23.                 </tr>  
  24.             </c:forEach>  
  25.         </table>  
  26.   </body>  
  27. </html>  

【orderMsg.jsp】

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"  
  2.       contentType="text/html;charset=UTF-8"%>  
  3. <%@ include file="/manage/header.jsp" %>  
  4.     订单号:${o.ordernum}  </br>金额:¥${o.price }元</br>  
  5.     明细如下:</br>  
  6.         <table border="1" width="88%" style="text-align:center" align="center">  
  7.             <tr>  
  8.                 <th>书名</th>  
  9.                 <th>作者</th>  
  10.                 <th>单价</th>  
  11.                 <th>小计</th>  
  12.                 <th>数量</th>  
  13.             </tr>  
  14.             <c:forEach items="${o.items }" var="i">  
  15.                 <tr>  
  16.                     <td>${i.book.name}</td>  
  17.                     <td>${i.book.author}</td>  
  18.                     <td>¥${i.book.price}元</td>  
  19.                     <td>${i.num}</td>  
  20.                     <td>¥${i.price}元</td>  
  21.                 </tr>  
  22.             </c:forEach>  
  23.         </table>  
  24.     收货信息如下:</br>  
  25.         地址:${o.user.address}</br>           
  26.         电话:${o.user.cellphone}</br>  
  27.         </hr>  
  28.         <c:if test="${o.state==0 }">  
  29.         <a href="${pageContext.request.contextPath}/manageServlet?operation=sureSend&ordersId=${o.id}">确认发货</a>  
  30.     </c:if>  
  31.   </body>  
  32. </html>  

【welcome.jsp】

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"  
  2.       contentType="text/html;charset=UTF-8"%>  
  3. <%@ include file="/manage/header.jsp" %>  
  4.     <h1 align="center">欢迎使用</h1>  
  5.   </body>  
  6. </html>  

3. 后台代码

后台代码管理员和客户两种基本角色各自一个Servlet解决:

客户servlet:ClientServlet

[java] view plain copy
  1. public class ClientServlet extends HttpServlet {  
  2.     private BusinessService bs = new BusinessServiceImpl();  
  3.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  4.             throws ServletException, IOException {  
  5.         String operation = request.getParameter("operation");  
  6.         if("showIndexCategory".equals(operation)){  
  7.             showIndexCategory(request,response);  
  8.         }  
  9.         if("showCategoryBook".equals(operation)){  
  10.             showCategoryBook(request,response);  
  11.         }  
  12.         if("buyBook".equals(operation)){  
  13.             buyBook(request,response);  
  14.         }  
  15.         if("genOrders".equals(operation)){  
  16.             genOrders(request,response);  
  17.         }  
  18.         if("regist".equals(operation)){  
  19.             regist(request,response);  
  20.         }  
  21.         if("login".equals(operation)){  
  22.             login(request,response);  
  23.         }  
  24.         if("logout".equals(operation)){  
  25.             logout(request,response);  
  26.         }  
  27.         if("showUsersOrders".equals(operation)){  
  28.             showUsersOrders(request,response);  
  29.         }  
  30.         if("showOrdersDetail".equals(operation)){  
  31.             showOrdersDetail(request,response);  
  32.         }  
  33.     }  
  34.     private void showOrdersDetail(HttpServletRequest request,  
  35.             HttpServletResponse response) throws ServletException, IOException {  
  36.         //1.判断用户是否登录。1.1没有登录,转向登录页面  
  37.         HttpSession session = request.getSession();  
  38.         User user = (User)session.getAttribute("user");  
  39.         if(user==null){  
  40.             request.setAttribute("message""请先登录!2秒后将自动转向登录页面。<meta http-equiv='Refresh' content='2;" +  
  41.                     "URL="+request.getContextPath()+"/client/login.jsp'/>");  
  42.             request.getRequestDispatcher("/client/message.jsp").forward(request, response);  
  43.             return ;  
  44.         }  
  45.         String ordersId=request.getParameter("ordersId");  
  46.         Orders o = bs.findOrdersById(ordersId);//★★查询的是订单明细,与通过用户id查订单是不同的,明细中还要查出书的信息  
  47.         request.setAttribute("o", o);  
  48.         request.getRequestDispatcher("/client/ordersMsg.jsp").forward(request, response);  
  49.     }  
  50.     //依据用户id查询用户订单  
  51.     private void showUsersOrders(HttpServletRequest request,  
  52.             HttpServletResponse response) throws ServletException, IOException {  
  53.         //1.判断用户是否登录。1.1没有登录,转向登录页面  
  54.         HttpSession session = request.getSession();  
  55.         User user = (User)session.getAttribute("user");  
  56.         if(user==null){  
  57.             request.setAttribute("message""请先登录!2秒后将自动转向登录页面。<meta http-equiv='Refresh' content='2;" +  
  58.                     "URL="+request.getContextPath()+"/client/login.jsp'/>");  
  59.             request.getRequestDispatcher("/client/message.jsp").forward(request, response);  
  60.             return ;  
  61.         }  
  62.         List<Orders> os = bs.findOrdersByUserId(user.getId());  
  63.         request.getSession().setAttribute("os", os);  
  64.         request.getRequestDispatcher("client/listOrders.jsp").forward(request,response);//转向主页  
  65.     }  
  66.     private void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  67.         request.getSession().invalidate();  
  68.         //注意:注销时是直接到根目录下  
  69.         request.getRequestDispatcher("/").forward(request, response);  
  70.     }  
  71.     private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  72.         String username = request.getParameter("username");  
  73.         String password = request.getParameter("password");  
  74.         User user = bs.login(username,password);  
  75.         if(user==null){  
  76.             request.setAttribute("message""用户名或密码错误!");  
  77.             request.getRequestDispatcher("/client/message.jsp").forward(request, response);  
  78.         }else{  
  79.             request.getSession().setAttribute("user", user);  
  80.             response.sendRedirect(request.getContextPath());//转向主页  
  81.         }  
  82.     }  
  83.     private void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  84.         User user = WebUtil.fillBean(request, User.class);  
  85.         if(!(user.getPassword()).equals(user.getPassword1())){//这里是比较  
  86.             request.setAttribute("message""两次密码输入不匹配");  
  87.             request.getRequestDispatcher("/client/message.jsp").forward(request, response);  
  88.         }  
  89.         bs.regist(user);  
  90.         request.setAttribute("message""注册成功");  
  91.         request.getRequestDispatcher("/client/message.jsp").forward(request, response);  
  92.     }  
  93.     //生成订单,把订单和订单项的信息存到数据库中  
  94.     private void genOrders(HttpServletRequest request,  
  95.             HttpServletResponse response) throws ServletException, IOException {  
  96.         //1.判断用户是否登录。1.1没有登录,转向登录页面  
  97.         HttpSession session = request.getSession();  
  98.         User user = (User)session.getAttribute("user");  
  99.         if(user==null){  
  100.             request.setAttribute("message""请先登录!2秒后将自动转向登录页面。<meta http-equiv='Refresh' content='2;" +  
  101.                     "URL="+request.getContextPath()+"/client/login.jsp'/>");  
  102.             request.getRequestDispatcher("/client/message.jsp").forward(request, response);  
  103.             return ;  
  104.         }  
  105.         //2.如果已经登录,则将购物车里面的东西按规则生成订单信息  
  106.         //取出购物车:Cart Map<String,CartItem>   
  107.         Cart cart  = (Cart)session.getAttribute("cart");//购物车<->订单  
  108.         Orders orders = new Orders();  
  109.         orders.setNum(cart.getNum());  
  110.         orders.setPrice(cart.getPrice());  
  111.         //★▲弄购物项  
  112.         List<OrdersItem> ordersItems = new ArrayList<OrdersItem>();//订单项<->订单项  
  113.         for(Map.Entry<String, CartItem> item:cart.getItems().entrySet()){//★▲▲填充模型  
  114.             CartItem i = item.getValue();//得到购物项  
  115.             OrdersItem orderItem = new OrdersItem();  
  116.             orderItem.setNum(i.getNum());  
  117.             orderItem.setPrice(i.getPrice());  
  118.             orderItem.setBook(i.getBook());  
  119.             ordersItems.add(orderItem);  
  120.         }  
  121.         orders.setItems(ordersItems);  
  122.         bs.addOrders(orders,user);  
  123.         //删除购物车  
  124.         request.getSession().removeAttribute("cart");  
  125.         request.setAttribute("message","订单生成成功!购物车为空");  
  126.         request.getRequestDispatcher("/client/message.jsp").forward(request, response);  
  127.     }  
  128.   
  129.     private void buyBook(HttpServletRequest request,  
  130.             HttpServletResponse response) throws ServletException, IOException {  
  131.         //购买书的id  
  132.         String bookId = request.getParameter("bookId");  
  133.         //获取要购买的书  
  134.         Book book = bs.findBookById(bookId);  
  135.         HttpSession session = request.getSession();  
  136.         //★▲从HttpSession中取出购物车  
  137.         Cart cart = (Cart)session.getAttribute("cart");  
  138.         //>创建购物车(判断是否有购物车)并放到httpsession中(购物车)  
  139.         if(cart==null){  
  140.             cart = new Cart();  
  141.             session.setAttribute("cart",cart);  
  142.         }  
  143.         //把书放到购物车中  
  144.         cart.addBook(book);  
  145.         //提示购买成功  
  146.         request.setAttribute("message""购买成功,2秒后返回主页面。<meta http-equiv='Refresh' content='2;" +  
  147.                     "URL="+request.getContextPath());  
  148.         //页面转发  
  149.         request.getRequestDispatcher("/client/message.jsp").forward(request, response);  
  150.     }  
  151.     //按照分类进行分页书籍查询  
  152.     private void showCategoryBook(HttpServletRequest request,  
  153.             HttpServletResponse response) throws ServletException, IOException {  
  154.         String pagenum = request.getParameter("pagenum");  
  155.         String categoryId = request.getParameter("categoryId");//★1.相对于下面那个方法再添加的  
  156.         Page page = bs.findPageRecords(pagenum,categoryId);//★2.相对于下面那个方法再添加的  
  157.         page.setUrl("/clientServlet?operation=showCategoryBook&categoryId="+categoryId);//★3.相对于下面那个方法再添加的  
  158.         request.setAttribute("page", page);  
  159.         request.getRequestDispatcher("/client/welcome.jsp").forward(request,response);  
  160.     }  
  161.     //查询所有分类,封装后,便于前端主页的显示  
  162.     private void showIndexCategory(HttpServletRequest request,  
  163.             HttpServletResponse response) throws ServletException, IOException {  
  164.         List<Category> cs = bs.findAllCategory();  
  165.         request.getSession().setAttribute("cs", cs);//★★用session放大范围以保持header.jsp部分的数据维护  
  166.         //查询所有书籍,还要分页  
  167.         String pagenum = request.getParameter("pagenum");  
  168.         Page page = bs.findPageRecords(pagenum);  
  169.         page.setUrl("/clientServlet?operation=showIndexCategory");  
  170.         request.setAttribute("page", page);  
  171.         request.getRequestDispatcher("/client/welcome.jsp").forward(request,response);  
  172.     }  
  173.   
  174.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  175.             throws ServletException, IOException {  
  176.         request.setCharacterEncoding("UTF-8");  
  177.         response.setContentType("text/html;charset=UTF-8");  
  178.         this.doGet(request, response);  
  179.     }  
  180. }  
  181. </span>  

  • 解决管理员基本功能的servlet:ManageServlet

[java] view plain copy
  1. public class ManageServlet extends HttpServlet {  
  2.     private BusinessService  bs = new BusinessServiceImpl();  
  3.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  4.             throws ServletException, IOException {  
  5.         String operation = request.getParameter("operation");  
  6.         if("addCategory".equals(operation))  
  7.             addCategory(request,response);  
  8.         if("listCategory".equals(operation))  
  9.             listCategory(request,response);  
  10.         if("showCategoryUI".equals(operation))  
  11.             showCategoryUI(request,response);  
  12.         if("addBook".equals(operation))  
  13.             addBook(request,response);  
  14.         if("showAllBook".equals(operation))  
  15.             showAllBook(request,response);  
  16.         if("showAllOrders0".equals(operation)){  
  17.             showAllOrders0(request,response);  
  18.         }  
  19.         if("showAllOrders1".equals(operation)){  
  20.             showAllOrders1(request,response);  
  21.         }  
  22.         if("showOrdersDetail".equals(operation)){  
  23.             showOrdersDetail(request,response);  
  24.         }  
  25.         if("sureSend".equals(operation)){  
  26.             sureSend(request,response);  
  27.         }  
  28.     }  
  29.     private void sureSend(HttpServletRequest request,  
  30.             HttpServletResponse response) throws ServletException, IOException {  
  31.         String ordersId = request.getParameter("ordersId");  
  32.         bs.sureOrders(ordersId);  
  33.         request.setAttribute("message","成功发货");  
  34.         request.getRequestDispatcher("/message.jsp").forward(request, response);  
  35.     }  
  36.     private void showOrdersDetail(HttpServletRequest request,  
  37.             HttpServletResponse response) throws ServletException, IOException {  
  38.         //哪个用户的也要查询出来  
  39.         String ordersId = request.getParameter("ordersId");  
  40.         Orders o = bs.findOrdersById(ordersId);  
  41.         request.setAttribute("o", o);  
  42.         request.getRequestDispatcher("/manage/ordersMsg.jsp").forward(request, response);  
  43.     }  
  44.     private void showAllOrders1(HttpServletRequest request,  
  45.             HttpServletResponse response) throws ServletException, IOException {  
  46.         List<Orders> os = bs.findOrdersByState(1);  
  47.         request.setAttribute("os", os);  
  48.         request.getRequestDispatcher("/manage/showOrders.jsp").forward(request, response);  
  49.     }  
  50.     //显示所有为发货的订单  
  51.     private void showAllOrders0(HttpServletRequest request,  
  52.             HttpServletResponse response) throws ServletException, IOException {  
  53.         List<Orders> os = bs.findOrdersByState(0);  
  54.         request.setAttribute("os", os);  
  55.         request.getRequestDispatcher("/manage/showOrders.jsp").forward(request, response);  
  56.     }  
  57.     //后台查询所有图书分页  
  58.     private void showAllBook(HttpServletRequest request,  
  59.             HttpServletResponse response) throws ServletException, IOException {  
  60.         String pagenum = request.getParameter("pagenum");  
  61.         Page page = bs.findPageRecords(pagenum);  
  62.         page.setUrl("manageServlet?operation=showAllBook");//第二种分页方式传递到jsp页面的数据  
  63.         request.setAttribute("page",page);  
  64.         request.getRequestDispatcher("/manage/listBook.jsp").forward(request, response);  
  65.     }  
  66.     //添加书籍到数据库:jsp页面传过来的是mutipart类型  
  67.     private void addBook(HttpServletRequest request,  
  68.             HttpServletResponse response) throws ServletException, IOException {  
  69.         String resultPath = "";  
  70.         String storePath = getServletContext().getRealPath("/files");  
  71.         try {  
  72.             Book book = new Book();  
  73.             DiskFileItemFactory factory = new DiskFileItemFactory();  
  74.             ServletFileUpload upload = new ServletFileUpload(factory);  
  75.             List<FileItem> items = upload.parseRequest(request);//获得待解析数据  
  76.             //解析数据  
  77.             for(FileItem item:items){  
  78.                 if(item.isFormField()){  
  79.                     //封装数据到javabean中  
  80.                     String filedName = item.getFieldName();//字段名,即javabean的属性名,除了图片  
  81.                     String filedValue = item.getString(request.getCharacterEncoding());//保证得到的字符串是UTF-8格式  
  82.                     BeanUtils.setProperty(book,filedName,filedValue);//到这一步,除了图片路径,其它数据都有了  
  83.                 }else{  
  84.                     //处理文件上传  
  85.                     InputStream in = item.getInputStream();  
  86.                     String fileName = item.getName();//用户上传文件路径名  
  87.                     //从用户上传的文件路径名中截取文件名。然后在加随机码,以防止用户上传的文件名相同发生冲突  
  88.                     fileName = UUID.randomUUID()+fileName.substring(fileName.lastIndexOf("\\")+1);  
  89.                     //设置存储的图片文件名  
  90.                     book.setImage(fileName);  
  91.                     OutputStream out = new FileOutputStream(storePath+"\\"+fileName);  
  92.                     byte b[] = new byte[1024];  
  93.                     int len = -1;  
  94.                     while((len=in.read(b))!=-1){  
  95.                         out.write(b,0,len);  
  96.                     }  
  97.                     out.close();  
  98.                     in.close();  
  99.                     item.delete();//★删除临时文件  
  100.                 }  
  101.             }  
  102.             //System.out.println(book);  
  103.             bs.addBook(book);//★★先测试,然后看方法对不对,在写service和dao层中写该方法    
  104.             List<Category> cs = bs.findAllCategory();  
  105.             request.setAttribute("cs", cs);//如果没有这两句,添加完成后添加页面将不会显示图书分类  
  106.             resultPath = "/manage/addBook.jsp";  
  107.             request.setAttribute("message","<script type='text/javascript'>alert('添加成功')</script>");  
  108.         } catch (Exception e) {  
  109.             e.printStackTrace();  
  110.             resultPath="/message.jsp";  
  111.             //TODO  
  112.             request.setAttribute("message""上传出问题了");  
  113.         }  
  114.         request.getRequestDispatcher(resultPath).forward(request, response);  
  115.     }  
  116.   
  117.     private void showCategoryUI(HttpServletRequest request,  
  118.             HttpServletResponse response) throws ServletException, IOException {  
  119.         List<Category> cs = bs.findAllCategory();  
  120.         request.setAttribute("cs", cs);  
  121.         request.getRequestDispatcher("/manage/addBook.jsp").forward(request, response);  
  122.     }  
  123.   
  124.     private void listCategory(HttpServletRequest request,  
  125.             HttpServletResponse response) throws ServletException, IOException {  
  126.         List<Category> cs = bs.findAllCategory();  
  127.         request.setAttribute("cs", cs);  
  128.         request.getRequestDispatcher("/manage/listCategory.jsp").forward(request, response);      
  129.     }  
  130.   
  131.     private void addCategory(HttpServletRequest request,  
  132.             HttpServletResponse response) throws ServletException, IOException {  
  133.         Category ca = WebUtil.fillBean(request, Category.class);  
  134.         bs.addCategory(ca);  
  135.         request.setAttribute("message""<script type='text/javascript'>alert('添加成功')</script>");  
  136.         request.getRequestDispatcher("/manage/addCategory.jsp").forward(request, response);       
  137.     }  
  138.   
  139.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  140.             throws ServletException, IOException {  
  141.         request.setCharacterEncoding("UTF-8");  
  142.         response.setContentType("text/html;charset=UTF-8");  
  143.         this.doGet(request, response);  
  144.   
  145.     }  
  146.   
  147. }  

展开阅读全文

没有更多推荐了,返回首页