jsp实验应用Servlet实现购物车讲解.docx

上传人:b****2 文档编号:406962 上传时间:2022-10-09 格式:DOCX 页数:11 大小:50.08KB
下载 相关 举报
jsp实验应用Servlet实现购物车讲解.docx_第1页
第1页 / 共11页
jsp实验应用Servlet实现购物车讲解.docx_第2页
第2页 / 共11页
jsp实验应用Servlet实现购物车讲解.docx_第3页
第3页 / 共11页
jsp实验应用Servlet实现购物车讲解.docx_第4页
第4页 / 共11页
jsp实验应用Servlet实现购物车讲解.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

jsp实验应用Servlet实现购物车讲解.docx

《jsp实验应用Servlet实现购物车讲解.docx》由会员分享,可在线阅读,更多相关《jsp实验应用Servlet实现购物车讲解.docx(11页珍藏版)》请在冰豆网上搜索。

jsp实验应用Servlet实现购物车讲解.docx

jsp实验应用Servlet实现购物车讲解

GoodsSingle

//保存商品名称

//保存商品价格//保存商品购买数量

应用Servlet实现购物车

具体实现过程

1、创建封装商品信息的值JavaBeanpackagecom.yxq.valuebean;

publicclassGoodsSingle{

privateStringname;

privatefloatprice;

privateintnum;

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){this.name=name;

}

publicintgetNum(){

returnnum;

}

publicvoidsetNum(intnum){

this.num=num;

publicfloatgetPrice(){returnprice;

}

publicvoidsetPrice(floatprice){this.price=price;

}

}

2、创建工具JavaBeanMyTools实现字符型数据转

换为整型及乱码处理

packagecom.yxq.toolbean;

importjava.io.UnsupportedEncodingException;

publicclassMyTools{

publicstaticintstrToint(Stringstr){//将String型数据

转换为int型数据的方法

if(str==null||str.equals(""))

str="0";

inti=0;

try{

int类型的变量

}catch(NumberFormatExceptione){//try-catch就

是监视try中的语句,如果抛出catch中声明的异常类型

i=0;

e.printStackTrace();//把Exception

的详细信息打印出来

}

returni;

}

publicstaticStringtoChinese(Stringstr){//进行转码操作

的方法

if(str==null)

str="";

try{

str=newString(str.getBytes("ISO-8859-1"),"gb2312");

}catch(UnsupportedEncodingExceptione){

str="";

e.printStackTrace();

}

returnstr;

}

3、创建购物车JavaBeanShopCar实现添加、删除,

购物车制作

packagecom.yxq.toolbean;

packagecom.yxq.toolbean;

importjava.util.ArrayList;

importcom.yxq.valuebean.GoodsSingle;

publicclassShopCar{

privateArrayListbuylist=newArrayList();//用来存储购买的商品

publicvoidsetBuylist(ArrayListbuylist){this.buylist=buylist;

}

/**

*@功能向购物车中添加商品

*@参数single为GoodsSingle类对象,圭寸装了要添加的商品信息

*/

publicvoidaddItem(GoodsSinglesingle){

if(single!

=null){

if(buylist.size()==0){

//如果buylist中不存在任何商品

GoodsSingletemp=newGoodsSingle();temp.setName(single.getName());temp.setPrice(single.getPrice());temp.setNum(single.getNum());buylist.add(temp);//存储商品

}

else{

//如果buylist中存在商品

inti=0;

for(;i

//遍历buylist集合对象,判断该集合中是否已经存在当前要添加的商品

GoodsSingletemp=(GoodsSingle)buylist.get(i);//获取buylist集合中当前元素

if(temp.getName().equals(single.getName())){

//判断从buylist集合中获取的当前商品的名称是否与要添加的商品的名称相同

//如果相同,说明已经购买了该商品,只需要

将商品的购买数量加1

temp.setNum(temp.getNum()+1);

//将商品购买数量加1

break;

//结束for循环

}

}

if(i>=buylist.size()){

//说明buylist中不存在要添加的商品GoodsSingletemp=newGoodsSingle();temp.setName(single.getName());temp.setPrice(single.getPrice());temp.setNum(single.getNum());buylist.add(temp);

//存储商品

}

}

}

}

/**

*@功能从购物车中移除指定名称的商品

*@参数name表示商品名称

*/

publicvoidremoveItem(Stringname){

for(inti=0;i

合,查找指定名称的商品

GoodsSingletemp=(GoodsSingle)buylist.get(i);

//获取集合中当前位置的商品if(temp.getName().equals(name)){//如果商品的名称为name参数指定的名称

if(temp.getNum()>1){//如果商品的购买数量大于1

temp.setNum(temp.getNum()-1);//

则将购买数量减1

break;//

结束for循环

}

elseif(temp.getNum()==1){//如果商品的购买数量为1

buylist.remove(i);//从buylist集合对象中移除该商品

}

4、创建实例首页面index.jsp,初始化商品信息

<%@pagecontentType="text/html;charset=gb2312"%>

forwardpage="/index"/>

5、创建处理用户访问首页面请求的Servlet---

IndexServlet

packagecom.yxq.servlet;

importjava.io.IOException;

importjava.util.ArrayList;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importjavax.servlet.http.HttpSession;

importcom.yxq.valuebean.GoodsSingle;

publicclassIndexServletextendsHttpServlet{

}

protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{

HttpSessionsession=request.getSession();session.setAttribute("goodslist",goodslist);response.sendRedirect("show.jsp");

}

static{//静态代码块

String^names={"苹果","香蕉","梨","橘子"};

float[]prices={2.8f,3.1f,2.5f,2.3f};

for(inti=0;i<4;i++){

GoodsSinglesingle=newGoodsSingle();

single.setName(names[i]);

single.setPrice(prices[i]);

single.setNum

(1);

goodslist.add(single);

}

}

6、show.jsp显示商品信息

<%@pagecontentType="text/html;charset=gb2312"%>

<%@pageimport="java.util.ArrayList"%>

<%@pageimport="com.yxq.valuebean.GoodsSingle"%>

<%ArrayListgoodslist=(ArrayList)session.getAttribute("goodslist");%>

提供商品如下

名称

价格(元/斤)v/td>

购买v/td>

<%if(goodslist==null||goodslist.size()==0){%>

没有商品可显示!

<%

else{

for(inti=0;ivgoodslist.size();i++){

GoodsSingle

single=(GoodsSingle)goodslist.get(i);

%>

vtrheight="50"align="center">

v%=single・getName()%x/td>

v%=single・getPrice()%x/td>

vtdxahref="doCar?

action=buy&id=v%=i%>">购

买v/a>v/td>v/tr>

v%

}

}

%>

查看购物车

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 英语

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1