1、WEB应用与开发网上购物系统课程设计报告 WEB应用与开发课程设计报 告设计题目: 网上购物系统一、设计时间 2016年5月 04日-6月08日总的设计时间为1周, 第17周。具体安排如下:1、分析设计准备阶段(第17周周一至周二) 2、编程调试阶段(第17周周三至第17周周四) 3、书写设计报告和书写说明书阶段(第17周周五) 4、考核阶段(第17周周五) 二、设计地点 信息科学与工程学院机房三、算法及流程图(一)功能模块的实现系统功能模块的划分前台系统顺序流程图1、大类别显示应用程序的首页只提供了一个Enter the Store的链接时,将导航到大类别页面,要完成这个过程,需要执行一下步
2、骤: (1)设置链接,为“Enter the Store”添加链接,代码如下: Enter the Store (2)设置配置文件,在web.xml中添加如下代码: IndexServlet org.bzc.jpetstore.servlets.IndexServlet IndexServlet /index.do (3)在src目录的orgbzcjpetstoreservlets文件夹中新建名为IndexServlet的类,Servlet本身并没有处理业务数据,而是调用CategoryBiz类的相关方法操作,具体代码如下: public class IndexServlet extends
3、HttpServlet public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException doPost(request, response);public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException CategoryBiz categorybiz = new Categ
4、oryBiz(); String tourl = ; /因为其他页面也需要获取大类别数据,所以存放于session中 HttpSession session = request.getSession(); /初始化一个List对象,用来存储大类别数据 List list = new ArrayList(); try /调用业务对象获取数据 list = categorybiz.searchById(0, ); tourl = /catalog/Main.jsp; catch (Exception e) tourl = index.html; e.printStackTrace(); sessi
5、on.setAttribute(categroyList, list); request.getRequestDispatcher(tourl).forward(request, response); (4)在src目录的orgbzcjpetstorebiz文件夹中新建名为CategoryBiz的类,CategoryBiz与数据库进行相互。此处需要查询的是所有的大类别数据,后面还需要根据大类别ID查询大类别数据,将这两部分整合,均由searchByld()方法提供这个功能。具体代码如下:public class CategoryBiz ControlDB controlDB = null; p
6、ublic CategoryBiz() controlDB = new ControlDB(); public List searchById(int flag, String catid) String sql = ; List list = new ArrayList(); if (flag = 0) sql = select * from category; else if (flag = 1) sql = select * from category where catid= + catid + ; System.out.println(sql); try list = control
7、DB.executeQueryCategory(sql); catch (Exception e) e.printStackTrace(); return list; (5)编写封装与数据库操作的ControlDB类。 (6)编写main.jsp页面,它用来显示大类别数据。main.jsp页面的部分代码如下:$category.name 运行Tomcat,执行此部分操作,最终效果如图所示:2、小类别显示,完成步骤同大类别显示运行Tomcat,执行此部分操作,最终效果如图所示:3、商品显示,完成步骤同大类别显示运行Tomcat,执行此部分操作,最终效果如图所示:4、添加商品到购物车在商品的列表页
8、面提供了添加到购物车的链接。单击Add to Cart链接可以把与之对应的商品添加入购物车中。要完成这个过程,需要执行一下步骤:(1)设置链接,为商品添加链接,在商品上创建链接的代码如下: (2)设置配置文件,在web.xml中添加如下代码: ItemServlet org.bzc.jpetstore.servlets.ItemServlet ItemServlet /item.do (3)在src目录的orgbzcjpetstoreservlets文件夹中新建名为ProductServlet的类,Servlet本身并没有处理业务数据,而是调用ItemBiz类的相关方法操作,具体代码如下:pu
9、blic class ProductServlet extends HttpServlet public void init() throws ServletException public void destroy() public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException doPost(request, response); public void doPost(HttpServletRequest request, Ht
10、tpServletResponse response) throws ServletException, IOException String path = request.getParameter(path); HttpSession session = request.getSession(); List listProduct = new ArrayList(); ProductBiz productbiz = new ProductBiz(); ItemBiz itembiz = new ItemBiz(); String tourl = ; if (show.equals(path)
11、 String productId = request.getParameter(productId); try List itemList = itembiz.searchByproductId(productId); Product product = (Product) productbiz.searchById(1, productId) .get(0); session.setAttribute(itemList, itemList); session.setAttribute(product, product); catch (Exception e) e.printStackTr
12、ace(); 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页面的部分代码如下:<< Main MenuShopping Cart Item
13、 ID Product ID Description In Stock? Quantity List Price Total Cost Your cart is empty. $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.listpr
14、ice $cartitem.total Sub Total: $cart.subTotal 运行Tomcat,执行此部分操作,最终效果如图所示:5、购物车中商品的管理在商品的列表页面提供了更改商品数量的输入框,用户可以更改数量,然后单击updatecart链接,完成更新购物车的操作,当操作执行完成后,返回本页面。单击remove链接可以把与之对应的商品从购物车中删除。(1)设置链接。如果用户执行“删除”操作,代码设置为:如果用户执行“修改”操作,将提交表单代码设置为:(2)设置配置文件。(3)在ItemServlet类中添加处理修改和删除功能的代码,具体如下:/获取商品标号Stri
15、ng workingItemId = request.getParameter(itemId);Cart cart = null;CartItem cartitem = null;/应该有个错误信息页跳转if (session.getAttribute(cartItems) = null) cart = new Cart(); else cart = (Cart) session.getAttribute(cart); /如果购物车中存在此商品,删除if (cart.containsItemId(workingItemId) cart.incrementQuantityByItemId(wor
16、kingItemId); else Item item = (Item) itembiz.searchById(1, workingItemId).get(0); cart.addItem(item, true); List cartItems = cart.getCartItemList(); session.setAttribute(cartItems, cartItems); session.setAttribute(cart, cart); tourl = /cart/Cart.jsp; else if (removeItemFromCart.equals(path) String w
17、orkingItemId = request.getParameter(workingItemId); Cart cart = null; CartItem cartitem = null; / 应该有个错误信息页跳转 if (session.getAttribute(cartItems) = null) tourl = /cart/Cart.jsp; request.getRequestDispatcher(tourl).forward(request, response); List cartItems = cart.getCartItemList(); session.setAttrib
18、ute(cartItems, cartItems); session.setAttribute(cart, cart); tourl = /cart/Cart.jsp; else if (updateCartQuantities.equals(path) Cart cart = null; CartItem cartitem = null; if (session.getAttribute(cartItems) = null) tourl = /cart/Cart.jsp; request.getRequestDispatcher(tourl).forward(request, respons
19、e); else cart = (Cart) session.getAttribute(cart); List cartItem = cart.getCartItemList(); / 定义一个map来接收页面上传来的所有值 Map parameterMap = new HashMap(); for (int i = 0; i cartItem.size(); i+) String key = cartItem.get(i).getItem().getItemid(); String value = request.getParameter(quantity + key); System.ou
20、t.println(value + *); parameterMap.put(key, value); / 调用修改数量的方法 cart.updateCartQuantities(parameterMap); List cartItems = cart.getCartItemList(); session.setAttribute(cartItems, cartItems); session.setAttribute(cart, cart); tourl = /cart/Cart.jsp; else if (checkout.equals(path) tourl = /cart/Checkout.jsp; else if (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、使用工具: