5、 创建处理用户访问首页面请求的Servlet--- IndexServlet
package com.yxq.servlet;
import java.io.IOException;
import java.util.ArrayList;
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.yxq.valuebean.GoodsSingle;
public class IndexServlet extends HttpServlet {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session=request.getSession();
session.setAttribute(\"goodslist\
response.sendRedirect(\"show.jsp\");
}
static{ //静态代码块
String[] names={\"苹果\香蕉\梨\橘子\
float[] prices={2.8f,3.1f,2.5f,2.3f};
for(int i=0;i<4;i++){
GoodsSingle single=new GoodsSingle();
single.setName(names[i]);
single.setPrice(prices[i]);
single.setNum(1);
goodslist.add(single);
}
}
}
6、 show.jsp显示商品信息
<%@ page contentType=\"text/html;charset=gb2312\"%>
<%@ page import=\"java.util.ArrayList\" %>
<%@ page import=\"com.yxq.valuebean.GoodsSingle\" %>
<% ArrayList goodslist=(ArrayList)session.getAttribute(\"goodslist\"); %>
cellpadding=\"0\">| 提供商品如下 |
| 名称 | 价格(元/斤) | 购买 |
<% if(goodslist==null||goodslist.size()==0){ %>
| 没有商品可显示! |
<%
}
else{
for(int i=0;iGoodsSingle single=(GoodsSingle)goodslist.get(i);%>
| <%=single.getName()%> | <%=single.getPrice()%> | \">购买 |
<%
}
}
%>
| 查看购物车 |
7、 创建处理用户购买、移除、清空购物车请求的Servlet Servlet----- BuyServlet
package com.yxq.servlet;
import java.io.IOException;
import java.util.ArrayList;
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.yxq.toolbean.MyTools;
import com.yxq.toolbean.ShopCar;
import com.yxq.valuebean.GoodsSingle;
public class BuyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action=request.getParameter(\"action\"); //获取action参数值
if(action==null)
action=\"\";
if(action.equals(\"buy\")) //触发了“购买”请求
buy(request,response); //调用buy()方法实现商品的购买
if(action.equals(\"remove\")) //触发了“移除”请求
remove(request,response); //调用remove()方法实现商品的移除
if(action.equals(\"clear\")) //触发了“清空购物车”请求
clear(request,response); //调用clear()方法实现购物车的清空
}
//实现购买商品的方法
protected void buy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session=request.getSession();
String strId=request.getParameter(\"id\"); //获取触发“购买”请求时传递
的id参数,该参数存储的是商品在goodslist对象中存储的位置
int id=MyTools.strToint(strId);
ArrayList goodslist=(ArrayList)session.getAttribute(\"goodslist\");
GoodsSingle single=(GoodsSingle)goodslist.get(id);
ArrayList buylist=(ArrayList)session.getAttribute(\"buylist\"); session范围内获取存储了用户已购买商品的集合对象
//从
if(buylist==null)
buylist=new ArrayList();
ShopCar myCar=new ShopCar();
myCar.setBuylist(buylist); 类实例中的属性
//将buylist对象赋值给ShopCar
myCar.addItem(single); 法实现商品添加操作
//调用ShopCar类中addItem()方
session.setAttribute(\"buylist\
response.sendRedirect(\"show.jsp\"); 面
//将请求重定向到show.jsp页
}
8、 在web.xml文件中配置Servlet
indexServlet
com.yxq.servlet.IndexServlet
indexServlet
/index
buyServlet
com.yxq.servlet.BuyServlet
buyServlet
/doCar
9、 创建页面shopcar.jsp购物车
<%@ page contentType=\"text/html;charset=gb2312\"%>
<%@ page import=\"java.util.ArrayList\" %>
<%@ page import=\"com.yxq.valuebean.GoodsSingle\" %>
<%
//获取存储在session中用来存储用户已购买商品的buylist集合对象
ArrayList buylist=(ArrayList)session.getAttribute(\"buylist\");
float total=0; //用来存储应付金额
%>
cellpadding=\"0\">| 购买的商品如下 |
| 名称 | 价格(元/斤) | 数量 | 总价(元) | 移除(-1/次) |
<% if(buylist==null||buylist.size()==0){ %>
| 您的购物车为空! |
<%
}
else{
for(int i=0;iGoodsSingle single=(GoodsSingle)buylist.get(i);String name=single.getName(); //获取商品名称
float price=single.getPrice(); //获取商品价格
int num=single.getNum(); //获取购买数量
//计算当前商品总价,并进行四舍五入
float money=((int)((price*num+0.05f)*10))/10f;
total+=money; //计算应付金额
%>
| <%=name%> | <%=price%> | <%=num%> | <%=money%> | href=\"doCar?action=remove&name=<%=single.getName() |
<%
%>\">
移
除
}
}
%>
| 应付金额 |
| 继续购物 | 清空购物车 |
:<%=total%>