jsp实验应用Servlet实现购物车讲解文档格式.docx

上传人:b****5 文档编号:18639467 上传时间:2022-12-30 格式:DOCX 页数:11 大小:18.42KB
下载 相关 举报
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

"

))

str="

0"

;

inti=0;

try{

i=Integer.parseInt(str);

//把str转换成int类型的变量

}catch(NumberFormatExceptione){//try-catch就是监视try中的语句,如果抛出catch中声明的异常类型

i=0;

e.printStackTrace();

//把Exception的详细信息打印出来

}

returni;

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

if(str==null)

try{

str=newString(str.getBytes("

ISO-8859-1"

),"

gb2312"

);

}catch(UnsupportedEncodingExceptione){

returnstr;

 

3、创建购物车JavaBean------ShopCar实现添加、删除,购物车制作

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.size();

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++){//遍历buylist集合,查找指定名称的商品

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"

%>

jsp:

forwardpage="

/index"

/>

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

packagecom.yxq.servlet;

importjava.io.IOException;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importjavax.servlet.http.HttpSession;

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};

4;

i++){

GoodsSinglesingle=newGoodsSingle();

single.setName(names[i]);

single.setPrice(prices[i]);

single.setNum

(1);

goodslist.add(single);

6、show.jsp显示商品信息

%@pageimport="

java.util.ArrayList"

%>

com.yxq.valuebean.GoodsSingle"

%ArrayListgoodslist=(ArrayList)session.getAttribute("

tableborder="

1"

width="

450"

rules="

none"

cellspacing="

cellpadding="

>

<

trheight="

50"

tdcolspan="

3"

align="

center"

提供商品如下<

/td>

/tr>

tralign="

height="

30"

bgcolor="

lightgrey"

<

td>

名称<

价格(元/斤)<

购买<

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

100"

没有商品可显示!

%

}

else{

for(inti=0;

goodslist.size();

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

%=single.getName()%>

%=single.getPrice()%>

ahref="

doCar?

action=buy&

id=<

%=i%>

/a>

%

tdalign="

colspan="

shopcar.jsp"

查看购物车<

/table>

7、创建处理用户购买、移除、清空购物车请求的ServletServlet-----BuyServlet

importcom.yxq.toolbean.MyTools;

importcom.yxq.toolbean.ShopCar;

publicclassBuyServletextendsHttpServlet{

protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{

doPost(request,response);

Stringaction=request.getParameter("

action"

//获取action参数值

if(action==null)

action="

if(action.equals("

buy"

))//触发了“购买”请求

buy(request,response);

//调用buy()方法实现商品的购买

remove"

))//触发了“移除”请求

remove(request,response);

//调用remove()方法实现商品的移除

clear"

))//触发了“清空购物车”请求

clear(request,response);

//调用clear()方法实现购物车的清空

//实现购买商品的方法

protectedvoidbuy(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{

StringstrId=request.getParameter("

id"

//获取触发“购买”请求时传递的id参数,该参数存储的是商品在goodslist对象中存储的位置

intid=MyTools.strToint(strId);

ArrayListgoodslist=(ArrayList)session.getAttribute("

GoodsSinglesingle=(GoodsSingle)goodslist.get(id);

ArrayListbuylist=(ArrayList)session.getAttribute("

buylist"

//从session范围内获取存储了用户已购买商品的集合对象

if(buylist==null)

buylist=newArrayList();

ShopCarmyCar=newShopCar();

myCar.setBuylist(buylist);

//将buylist对象赋值给ShopCar类实例中的属性

myCar.addItem(single);

//调用ShopCar类中addItem()方法实现商品添加操作

buylist);

//将请求重定向到show.jsp页面

8、在web.xml文件中配置Servlet

?

xmlversion="

1.0"

encoding="

UTF-8"

web-app>

!

--配置IndexServlet-->

servlet>

servlet-name>

indexServlet<

/servlet-name>

servlet-class>

com.yxq.servlet.IndexServlet<

/servlet-class>

/servlet>

servlet-mapping>

url-pattern>

/index<

/url-pattern>

/servlet-mapping>

--配置BuyServlet-->

buyServlet<

com.yxq.servlet.BuyServlet<

/doCar<

/web-app>

9、创建页面shopcar.jsp购物车

//获取存储在session中用来存储用户已购买商品的buylist集合对象

ArrayListbuylist=(ArrayList)session.getAttribute("

floattotal=0;

//用来存储应付金额

5"

购买的商品如下<

tdwidth="

25%"

数量<

总价(元)<

移除(-1/次)<

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

您的购物车为空!

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

Stringname=single.getName();

//获取商品名称

floatprice=single.getPrice();

//获取商品价格

intnum=single.getNum();

//获取购买数量

//计算当前商品总价,并进行四舍五入

floatmoney=((int)((price*num+0.05f)*10))/10f;

total+=money;

//计算应付金额

%=name%>

%=price%>

%=num%>

%=money%>

action=remove&

name=<

%=single.getName()%>

移除<

%

应付金额:

%=total%>

2"

继续购物<

action=clear"

清空购物车<

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

当前位置:首页 > 工作范文 > 制度规范

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

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