您好,欢迎访问三七文档
JSP作业4-2编写一个实现页面计数的计数器,要求当刷新页面时,不增加计数。程序代码%@pagecontentType=text/html;charset=gb2312%%!intnum=0;synchronizedvoidadd(){num++;}%htmlheadtitle计数器/title/headbody%if(session.isNew())add();%center您是第%=num%位访问该页的游客!/center/body/html截图4-3编写一个简易购物车,实现向购物车内添加商品,移除指定商品及清空购物车功能。程序代码%@pagecontentType=text/html;charset=gb2312%%@pageimport=java.util.ArrayList%%!intm=0;intId=8888;ArrayListStringgoodslist=newArrayListString();ArrayListFloatpgoodslist=newArrayListFloat();ArrayListStringbuylist=newArrayListString();ArrayListFloatpbuylist=newArrayListFloat();ArrayListIntegernbuylist=newArrayListInteger();%%floattotal=0;%%if(Id==8888){goodslist.add(0,荔枝);goodslist.add(1,火龙果);goodslist.add(2,西瓜);goodslist.add(3,柚子);pgoodslist.add(0,8.8f);pgoodslist.add(1,9.6f);pgoodslist.add(2,5.2f);pgoodslist.add(3,4.8f);}%%session.setAttribute(goodslist,goodslist);session.setAttribute(pgoodslist,pgoodslist);%tableborder=1width=450rules=nonecellspacing=0cellpadding=0trheight=50tdcolspan=3align=center提供商品如下/td/trtd名称/tdtd价格(元/斤)/tdtd购买/td%session.getAttribute(goodslist);session.getAttribute(pgoodslist);%%if(goodslist==null||goodslist.size()==0){%trheight=100tdcolspan=3align=center没有商品可以显示/td/tr%}else{for(intm=0;mgoodslist.size();m++){%trheight=50align=centertd%=goodslist.get(m)%/tdtd%=pgoodslist.get(m)%/tdtdahref=index.jsp?id=%=m+1%购买/a/td/tr%}}%/table**************************************************************************tableborder=1width=450rules=nonecellspacing=0cellpadding=0tralign=centerheight=30bgcolor=lightgreytd名称/tdtd价格/tdtd数量/tdtd总价/tdtd移除/td/tr%Stringstr=request.getParameter(id);if(str==null||str.equals())str=0;try{Id=Integer.parseInt(str);}catch(NumberFormatExceptione){Id=0;e.printStackTrace();}%%if(Id==1){if(buylist.size()==0){buylist.add(0,goodslist.get(0));pbuylist.add(0,pgoodslist.get(0));nbuylist.add(0,1);}else{intk=0;for(;kbuylist.size();k++){if(buylist.get(k).equals(goodslist.get(0))){intw=nbuylist.get(k);nbuylist.set(k,w+1);break;}}if(k=buylist.size()){buylist.add(k,goodslist.get(0));pbuylist.add(k,pgoodslist.get(0));nbuylist.add(k,1);}}}if(Id==2){if(buylist.size()==0){buylist.add(0,goodslist.get(1));pbuylist.add(0,pgoodslist.get(1));nbuylist.add(0,1);}else{intk=0;for(;kbuylist.size();k++){if(buylist.get(k).equals(goodslist.get(1))){intw=nbuylist.get(k);nbuylist.set(k,w+1);break;}}if(k=buylist.size()){buylist.add(k,goodslist.get(1));pbuylist.add(k,pgoodslist.get(1));nbuylist.add(k,1);}}}if(Id==3){if(buylist.size()==0){buylist.add(0,goodslist.get(2));pbuylist.add(0,pgoodslist.get(2));nbuylist.add(0,1);}else{intk=0;for(;kbuylist.size();k++){if(buylist.get(k)==goodslist.get(2)){intw=nbuylist.get(k);nbuylist.set(k,w+1);break;}}if(k=buylist.size()){buylist.add(k,goodslist.get(2));pbuylist.add(k,pgoodslist.get(2));nbuylist.add(k,1);}}}if(Id==4){if(buylist.size()==0){buylist.add(0,goodslist.get(3));pbuylist.add(0,pgoodslist.get(3));nbuylist.add(0,1);}else{intk=0;for(;kbuylist.size();k++){if(buylist.get(k)==goodslist.get(3)){intw=nbuylist.get(k);nbuylist.set(k,w+1);break;}}if(k=buylist.size()){buylist.add(k,goodslist.get(3));pbuylist.add(k,pgoodslist.get(3));nbuylist.add(k,1);}}}if(Id==10){if(nbuylist.get(0)1){inth=nbuylist.get(0);nbuylist.set(0,h-1);}elseif(nbuylist.get(0)==1){buylist.remove(0);pbuylist.remove(0);nbuylist.remove(0);}}if(Id==20){if(nbuylist.get(1)1){inth=nbuylist.get(1);nbuylist.set(1,h-1);}elseif(nbuylist.get(1)==1){buylist.remove(1);pbuylist.remove(1);nbuylist.remove(1);}}if(Id==30){if(nbuylist.get(2)1){inth=nbuylist.get(2);nbuylist.set(2,h-1);}elseif(nbuylist.get(2)==1){buylist.remove(2);pbuylist.remove(2);nbuylist.remove(2);}}if(Id==40){if(nbuylist.get(3)1){inth=nbuylist.get(3);nbuylist.set(3,h-1);}elseif(nbuylist.get(3)==1){buylist.remove(3);pbuylist.remove(3);nbuylist.remove(3);}}if(Id==888888){buylist.clear();pbuylist.clear();nbuylist.clear();total=0;}if(buylist==null||buylist.size()==0){%trheight=100tdcolspan=5align=center您的购物车为空/td/tr%}else{for(intl=0;lbuylist.size();l++){Stringname=buylist.get(l);floatprice=(float)pbuylist.get(l);intnum=(int)nbuylist.get(l);floatmoney=((int)((price*num+0.05f)*10))/10f;total+=money;%tralign=centerheight=50td%=name%/tdtd%=price%/tdtd%=num%/tdtd%=money%/tdtdahref=index.jsp?id=%=((l+1)*10)%移除/a/td/tr%}}%trheight=50align=centertdcolspan=5应付金额:%=total%/td/trtrheight=50align=centertdcolspan=3ahref=index.jsp?id=888888清空购物车/a/td/tr/table截图5-3实现一个简单的登录程序。要求应用JavaBean来接收用户输入的用户名和密码,然后判断输入的用户名是否为“admin”,密码是否为“000”;若是,则转发到success.jsp页面显示“欢迎登录”提示信息,否则转发到fault.jsp页面显示“登录失败”提示信息。程序代码:User.java文件:packagecom.yxq.bean;publicclassUser{privateStringusername;privateStringuserpass;publicStringgetUsername(){returnusername;}publicvoidsetUsername(Stringusername){this.username=username;}publicStringgetUserpass(){returnuserpass;}publicvoidsetUserpass(Stringus
本文标题:JSP作业(二)
链接地址:https://www.777doc.com/doc-2882181 .html