首页 > 代码库 > 购物车模块
购物车模块
学习开发购物车模块
发现一个问题,如果web.xml中配置映射路径中/servlet/***,在serlet中,跳转的时候,会在路径中自动添加/servlet,这个真是非常的恶心。下次设置映射的时候,不加/servlet.
首先给出购买页。这里用静态页面。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="http://www.mamicode.com/"><title>My JSP ‘shoMerchandise.jsp‘ starting page</title></head><body> <table width=424 " height="583" border="0" align="center"> <tr> <td width="190"><img alt="hpcomputer" src="images/hppavilion.jpg"></td> <td width="224"><img src="http://www.mamicode.com/images/lenovo.jpg"> <td> </tr> <tr> <td>笔记本电脑</td> <td>移动硬盘 </tr> <tr> <td>价格:5999.99元</td> <td>价格:688.88</td> </tr> <tr> <td> <a href="http://www.mamicode.com/servlet/AddShoppingCart?id=0001&name=笔记本电脑&price=5999.00"><img src="http://www.mamicode.com/images/buybutton.gif" width="71" height="21" border="0" /></a> </td> <td> <a href="http://www.mamicode.com/servlet/AddShoppingCart?id=0002&name=移动硬盘&price=688.88"><img src="http://www.mamicode.com/images/buybutton.gif" width="71" height="21" border="0"/></a> </tr> </table></body></html>
点击购买,跳转到addShoppingCart.java,这是一个servlet,处理的事件为把点击购买的商品添加到购物车中:
过程是,首先从session中读取shoppingcart,如果为空,则一个购物车类(通过javabean:ShoppingCart类),然后在session中新建一个购物车的信息。
session.setAttribute("shoppingcart", cart);
否则的话,就从session中读取出shoppingcart的信息。保存在javabean:ShoppingCart new cart中。
然后接收request传递到服务器的信息,并验证信息的合法性。
//获得商品的基本信息 String id=request.getParameter("id"); String name=request.getParameter("name"); byte source [] = name.getBytes("iso8859-1"); name = new String (source,"UTF-8"); String quantity=request.getParameter("q"); String price = request.getParameter("price"); System.out.println(id+name+quantity+price); //检验要加入的商品信息 if(StringTool.validateNull(id)||StringTool.validateNull("name")||StringTool.validateNull("price")||StringTool.validateNull("quantity")){ printError(request,response); return; } id=StringTool.filterHtml(id); name=StringTool.filterHtml(name);
之后,执行cart的添加商品javebean:CartItem的addItem方法,把商品添加到cart中去。
try{ if(StringTool.validateNull(quantity)){ //根据传入的商品数据创建商品,然后利用addCartItem方法添加进购物车 cart.addItem(new CartItem(id,name,1,Double.parseDouble(price))); System.out.println(Double.parseDouble(price)); }else{ cart.addItem(new CartItem(id,name,Integer.parseInt(quantity),Double.parseDouble(price))); } }catch(NumberFormatException e){ printError(request,response); return; }
这里并没有更新session,我这样理解:cart是建立的一个指针,它们之间(cart、session)是浅复制,所以不必更新session。
跳转到展示购物车的页面。
response.sendRedirect("GetShoppingCart");
这里就是我说的自动添加/servlet那个问题之一,按理说,按照映射的话,应该是/servlet/×××,但是由于是在servlet中跳转的,所以会自动在跳转路径中添加/servlet。
可以自己试一下。
如果商品添加失败,在输出错误信息。
下面是完整代码:AddShoppingCart.java
package com.cjg.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import com.cjg.bean.CartItem;import com.cjg.bean.ShoppingCart;import com.cjg.tools.*;public class AddShoppingCart extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); doPost(request,response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); response.setContentType("text/html;charset:utf-8"); HttpSession session = request.getSession();//获取session ShoppingCart cart=(ShoppingCart) session.getAttribute("shoppingcart"); //当购物车为空时 if(cart==null){ cart=new ShoppingCart(); session.setAttribute("shoppingcart", cart); } //获得商品的基本信息 String id=request.getParameter("id"); String name=request.getParameter("name"); byte source [] = name.getBytes("iso8859-1"); name = new String (source,"UTF-8"); String quantity=request.getParameter("q"); String price = request.getParameter("price"); System.out.println(id+name+quantity+price); //检验要加入的商品信息 if(StringTool.validateNull(id)||StringTool.validateNull("name")||StringTool.validateNull("price")||StringTool.validateNull("quantity")){ printError(request,response); return; } id=StringTool.filterHtml(id); name=StringTool.filterHtml(name); try{ if(StringTool.validateNull(quantity)){ //根据传入的商品数据创建商品,然后利用addCartItem方法添加进购物车 cart.addItem(new CartItem(id,name,1,Double.parseDouble(price))); System.out.println(Double.parseDouble(price)); }else{ cart.addItem(new CartItem(id,name,Integer.parseInt(quantity),Double.parseDouble(price))); } }catch(NumberFormatException e){ printError(request,response); return; } response.sendRedirect("GetShoppingCart"); //System.out.println("ssss"); } //商品添加失败的输出信息 private void printError(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException { response.setContentType("text/html;charset:utf-8"); PrintWriter out =response.getWriter(); out.println("<html>"); out.println("<head><title>购买商品失败</title></head>"); out.println("<body>"); out.println("<h1>缺少商品参数或者商品参数不正确,添加商品到购物车中不成功</h1><br><br>"); out.println("<a href=http://www.mamicode.com/"/onlineshoppingcart/showMerchandise.htm\">继续浏览商品,添加商品到购物车</a><br>"); out.println("</body>"); out.println("</html>"); out.flush(); out.close(); }}
购物车模块