jsp实验应用Servlet实现购物车讲解Word格式.docx
- 文档编号:13335319
- 上传时间:2022-10-09
- 格式:DOCX
- 页数:11
- 大小:50.08KB
jsp实验应用Servlet实现购物车讲解Word格式.docx
《jsp实验应用Servlet实现购物车讲解Word格式.docx》由会员分享,可在线阅读,更多相关《jsp实验应用Servlet实现购物车讲解Word格式.docx(11页珍藏版)》请在冰豆网上搜索。
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)
try{
str=newString(str.getBytes("
ISO-8859-1"
),"
gb2312"
);
}catch(UnsupportedEncodingExceptione){
returnstr;
3、创建购物车JavaBeanShopCar实现添加、删除,
购物车制作
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中存在商品
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();
//存储商品
*@功能从购物车中移除指定名称的商品
*@参数name表示商品名称
publicvoidremoveItem(Stringname){
for(inti=0;
i++){//遍历buylist集
合,查找指定名称的商品
//获取集合中当前位置的商品if(temp.getName().equals(name)){//如果商品的名称为name参数指定的名称
if(temp.getNum()>
1){//如果商品的购买数量大于1
temp.setNum(temp.getNum()-1);
//
则将购买数量减1
结束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;
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>
名称<
价格(元/斤)v/td>
购买v/td>
%if(goodslist==null||goodslist.size()==0){%>
100"
没有商品可显示!
%
ivgoodslist.size();
single=(GoodsSingle)goodslist.get(i);
vtrheight="
v%=single・getName()%x/td>
v%=single・getPrice()%x/td>
vtdxahref="
doCar?
action=buy&
id=v%=i%>
购
买v/a>
v/td>
v/tr>
v%
trheight="
>
tdalign="
colspan="
ahref="
shopcar.jsp"
查看购物车<
/a>
/td>
/tr>
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- jsp 实验 应用 Servlet 实现 购物 讲解