《WEB应用与开发》网上购物系统课程设计报告.docx
《《WEB应用与开发》网上购物系统课程设计报告.docx》由会员分享,可在线阅读,更多相关《《WEB应用与开发》网上购物系统课程设计报告.docx(13页珍藏版)》请在冰豆网上搜索。
《WEB应用与开发》网上购物系统课程设计报告
WEB应用与开发课程设计
报告
设计题目:
网上购物系统
一、设计时间
2016年5月04日-----6月08日
总的设计时间为1周,第17周。
具体安排如下:
1、分析设计准备阶段(第17周周一至周二)
2、编程调试阶段(第17周周三至第17周周四)
3、书写设计报告和书写说明书阶段(第17周周五)
4、考核阶段(第17周周五)
二、设计地点
信息科学与工程学院机房
三、算法及流程图
(一)功能模块的实现
系统功能模块的划分
前台系统顺序流程图
1、大类别显示
应用程序的首页只提供了一个EntertheStore的链接时,将导航到大类别页面,要完成这个过程,需要执行一下步骤:
(1)设置链接,为“EntertheStore”添加链接,代码如下:
EntertheStore
(2)设置配置文件,在web.xml中添加如下代码:
IndexServlet
org.bzc.jpetstore.servlets.IndexServlet
IndexServlet
/index.do
(3)在src目录的org\bzc\jpetstore\servlets文件夹中新建名为IndexServlet的类,Servlet本身并没有处理业务数据,而是调用CategoryBiz类的相关方法操作,具体代码如下:
publicclassIndexServletextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,
HttpServletResponseresponse)
throwsServletException,IOException{
doPost(request,response);}
publicvoiddoPost(HttpServletRequestrequest,
HttpServletResponseresponse)
throwsServletException,IOException{
CategoryBizcategorybiz=newCategoryBiz();
Stringtourl="";
//因为其他页面也需要获取大类别数据,所以存放于session中
HttpSessionsession=request.getSession();
//初始化一个List对象,用来存储大类别数据
Listlist=newArrayList();
try{
//调用业务对象获取数据
list=categorybiz.searchById(0,"");
tourl="/catalog/Main.jsp";}
catch(Exceptione){
tourl="index.html";
e.printStackTrace();}
session.setAttribute("categroyList",list);
request.getRequestDispatcher(tourl).forward(request,response);
}
}
(4)在src目录的org\bzc\jpetstore\biz文件夹中新建名为CategoryBiz的类,CategoryBiz与数据库进行相互。
此处需要查询的是所有的大类别数据,后面还需要根据大类别ID查询大类别数据,将这两部分整合,均由searchByld()方法提供这个功能。
具体代码如下:
publicclassCategoryBiz{
ControlDBcontrolDB=null;
publicCategoryBiz(){
controlDB=newControlDB();
}
publicListsearchById(intflag,Stringcatid){
Stringsql="";
Listlist=newArrayList();
if(flag==0){
sql="select*fromcategory";
}elseif(flag==1){
sql="select*fromcategorywherecatid='"+catid+"'";
}
System.out.println(sql);
try
{list=controlDB.executeQueryCategory(sql);}
catch(Exceptione)
{e.printStackTrace();}
returnlist;
}
}
(5)编写封装与数据库操作的ControlDB类。
(6)编写main.jsp页面,它用来显示大类别数据。
main.jsp页面的部分代码如下:
……
forEachitems="${categroyList}"var="category">
path=show&categoryId=${category.catid}"> outvalue="${category.descn}"escapeXml="false"/>
${category.name}
|
forEach>
运行Tomcat,执行此部分操作,最终效果如图所示:
2、小类别显示,完成步骤同大类别显示
运行Tomcat,执行此部分操作,最终效果如图所示:
3、商品显示,完成步骤同大类别显示
运行Tomcat,执行此部分操作,最终效果如图所示:
4、添加商品到购物车
在商品的列表页面提供了添加到购物车的链接。
单击AddtoCart链接可以把与之对应的商品添加入购物车中。
要完成这个过程,需要执行一下步骤:
(1)设置链接,为商品添加链接,在商品上创建链接的代码如下:
path=addItemToCart&itemId=${item.itemid}&product”>
(2)设置配置文件,在web.xml中添加如下代码:
ItemServlet
org.bzc.jpetstore.servlets.ItemServlet
ItemServlet
/item.do
(3)在src目录的org\bzc\jpetstore\servlets文件夹中新建名为ProductServlet的类,Servlet本身并没有处理业务数据,而是调用ItemBiz类的相关方法操作,具体代码如下:
publicclassProductServletextendsHttpServlet{
publicvoidinit()throwsServletException{}
publicvoiddestroy(){}
publicvoiddoGet(HttpServletRequestrequest,
HttpServletResponseresponse)
throwsServletException,IOException{
doPost(request,response);
}
publicvoiddoPost(HttpServletRequestrequest,
HttpServletResponseresponse)
throwsServletException,IOException{
Stringpath=request.getParameter("path");
HttpSessionsession=request.getSession();
ListlistProduct=newArrayList();
ProductBizproductbiz=newProductBiz();
ItemBizitembiz=newItemBiz();
Stringtourl="";
if("show".equals(path)){
StringproductId=request.getParameter("productId");
try{
List- itemList=itembiz.searchByproductId(productId);
Productproduct=(Product)productbiz.searchById(1,productId)
.get(0);
session.setAttribute("itemList",itemList);
session.setAttribute("product",product);
}catch(Exceptione){
e.printStackTrace();}
tourl="/catalog/Product.jsp";
}else{
listProduct=productbiz.searchById(0,"");
tourl="index.html";
session.setAttribute("listProduct",listProduct);
}
request.getRequestDispatcher(tourl).forward(request,response);
}
}
(4)编写Cart.jsp页面,来显示购物车中的商品项数据,Cart.jsp页面的部分代码如下:
<<MainMenu
|
ShoppingCart
path=updateCartQuantities">
ItemID | ProductID | Description | InStock? | Quantity | ListPrice | TotalCost | |
iftest="${cart.numberOfItems==0}">
Yourcartisempty.
if>
forEachitems="${cartItems}"var="cartitem">
path=show&itemId=${cartitem.item.itemid}"> ${cartitem.item.itemid}
|
${cartitem.item.productid} | ${cartitem.item.attr1} ${cartitem.item.attr2} ${cartitem.item.attr3} ${cartitem.item.attr4} ${cartitem.item.attr5} ${product.name} | ${cartitem.inStock}
${cartitem.item.listprice}
${cartitem.total}
path=removeItemFromCart&workingItemId=${cartitem.item.itemid}">
| forEach>
SubTotal:
${cart.subTotal}
|
运行Tomcat,执行此部分操作,最终效果如图所示:
5、购物车中商品的管理
在商品的列表页面提供了更改商品数量的输入框,用户可以更改数量,然后单击updatecart链接,完成更新购物车的操作,当操作执行完成后,返回本页面。
单击remove链接可以把与之对应的商品从购物车中删除。
(1)设置链接。
如果用户执行“删除”操作,代码设置为:
path=addItemToCart&itemId=${item.itemid}&product”>
如果用户执行“修改”操作,将提交表单代码设置为:
/item.do?
path=updateCartQuantities”>
(2)设置配置文件。
(3)在ItemServlet类中添加处理修改和删除功能的代码,具体如下:
//获取商品标号
StringworkingItemId=request.getParameter("itemId");
Cartcart=null;
CartItemcartitem=null;
//应该有个错误信息页跳转
if(session.getAttribute("cartItems")==null){
cart=newCart();
}else{
cart=(Cart)session.getAttribute("cart");
}
//如果购物车中存在此商品,删除
if(cart.containsItemId(workingItemId)){
cart.incrementQuantityByItemId(workingItemId);
}else{
Itemitem=(Item)itembiz.searchById(1,workingItemId).get(0);
cart.addItem(item,true);
}
ListcartItems=cart.getCartItemList();
session.setAttribute("cartItems",cartItems);
session.setAttribute("cart",cart);
tourl="/cart/Cart.jsp";
}elseif("removeItemFromCart".equals(path)){
StringworkingItemId=request.getParameter("workingItemId");
Cartcart=null;
CartItemcartitem=null;
//应该有个错误信息页跳转
if(session.getAttribute("cartItems")==null){
tourl="/cart/Cart.jsp";
request.getRequestDispatcher(tourl).forward(request,response);}
ListcartItems=cart.getCartItemList();
session.setAttribute("cartItems",cartItems);
session.setAttribute("cart",cart);
tourl="/cart/Cart.jsp";
}elseif("updateCartQuantities".equals(path)){
Cartcart=null;
CartItemcartitem=null;
if(session.getAttribute("cartItems")==null){
tourl="/cart/Cart.jsp";
request.getRequestDispatcher(tourl).forward(request,response);
}else{cart=(Cart)session.getAttribute("cart");}
ListcartItem=cart.getCartItemList();
//定义一个map来接收页面上传来的所有值
MapparameterMap=newHashMap();
for(inti=0;iStringkey=cartItem.get(i).getItem().getItemid();
Stringvalue=request.getParameter("quantity"+key);
System.out.println(value+"**********************");
parameterMap.put(key,value);
}
//调用修改数量的方法
cart.updateCartQuantities(parameterMap);
ListcartItems=cart.getCartItemList();
session.setAttribute("cartItems",cartItems);
session.setAttribute("cart",cart);
tourl="/cart/Cart.jsp";
}elseif("checkout".equals(path)){
tourl="/cart/Checkout.jsp";
}elseif("viewCart".equals(path)){
tourl="/cart/Cart.jsp";
}else{
listItem=itembiz.searchById(0,"");
tourl="index.html";
session.setAttribute("listItem",listItem);}
request.getRequestDispatcher(tourl).forward(request,response);
}
}
(二)运行工程
1、使用工具: