jsp实验3

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Javaweb应用编程基础实验报告实验3:内置对象、Cookie实验一、实验目的:理解对象概念,掌握application、session等对象的基本使用方法。理解Cookie概念,掌握Cookie的基本使用方法。二、实验要求:使用专业开发工具MyEclipse编程。上交实验报告电子文档(文档包含源程序,以班级、学号、姓名依次出现组成的字符串标识文档,以班级为单位上交)。三、实验内容:1、编写一个简单的网上商店,用application对象存放网上的商品清单和库存量,用session对象存放用户的购买信息(用户名、商品名及购买数量),然后利用这些信息给用户结账。2、利用Cookie实现用户的自动登录功能。其中,要求在登录页面中通过对Cookie有效期的设置来保存用户的信息。四、实验过程中遇到的问题及解决手段:在做网上商店时,若不购买某一商品即购买数量为空时,无法通过参数传递,导致异常,想了很久,最后只能将购买数量默认为0,这样才能满足不购买的时候不会显示该商品并且不会显示异常。试过用中文命名文件,但会出现乱码。五、程序源代码:1.源代码文件:login.jsp,application.jsp,session.jsplogin.jsp%@pagelanguage=javacontentType=text/html;charset=UTF-8pageEncoding=UTF-8%!DOCTYPEhtmlPUBLIC-//W3C//DTDHTML4.01Transitional//EN=Content-Typecontent=text/html;charset=UTF-8title登录/title/headbodyformname=form1method=postaction=application.jsppalign=center用户名:inputtype=textname=username/ppalign=center密 码:inputtype=passwordname=password/ppalign=centerinputtype=submitname=submitvalue=提交  inputtype=resetname=cancelid=cancelvalue=取消/p/form/body/htmlapplication.jsp%@pagelanguage=javacontentType=text/html;charset=UTF-8pageEncoding=UTF-8%%request.setCharacterEncoding(UTF-8);%%!inta1=30,a2=25,a3=40;doublep1=48.00,p2=58.00,p3=45.00;Strings1=语文,s2=数学,s3=英语;%!DOCTYPEhtmlPUBLIC-//W3C//DTDHTML4.01Transitional//EN=Content-Typecontent=text/html;charset=UTF-8title请求/title/headbodyformmethod=postaction=session.jsptabletralign=centerth商品名/thth价格/元/thth库存量/thth购买数量/th/trtralign=leftth%=s1%/thtd%=p1%/tdtd%=a1%/tdtdinputtype=textname=num1value=0/td/trtralign=leftth%=s2%/thtd%=p2%/tdtd%=a2%/tdtdinputtype=textname=num2value=0/td/trtralign=leftth%=s3%/thtd%=p3%/tdtd%=a3%/tdtdinputtype=textname=num3value=0/tdtdinputtype=submitname=submitvalue=提交/td/tr/table%application.setAttribute(s1,s1);application.setAttribute(s2,s2);application.setAttribute(s3,s3);application.setAttribute(p1,p1);application.setAttribute(p2,p2);application.setAttribute(p3,p3);Stringusername=request.getParameter(username);session.setAttribute(username,username);%/form/body/htmlsession.jsp%@pagelanguage=javacontentType=text/html;charset=UTF-8pageEncoding=UTF-8%!DOCTYPEhtmlPUBLIC-//W3C//DTDHTML4.01Transitional//EN=Content-Typecontent=text/html;charset=UTF-8titleInserttitlehere/title/headbodyp您好!%=session.getAttribute(username)%/pbrp您购买的商品如下:/pbr%Objectid1=application.getAttribute(s1);Objectid2=application.getAttribute(s2);Objectid3=application.getAttribute(s3);Objectstr1=application.getAttribute(p1);doubleg1=Double.parseDouble(str1.toString());Objectstr2=application.getAttribute(p2);doubleg2=Double.parseDouble(str2.toString());Objectstr3=application.getAttribute(p3);doubleg3=Double.parseDouble(str3.toString());Stringnum1=request.getParameter(num1);Stringnum2=request.getParameter(num2);Stringnum3=request.getParameter(num3);session.setAttribute(num1,num1);session.setAttribute(num2,num2);session.setAttribute(num3,num3);Objectm1=session.getAttribute(num1);intn1=Integer.parseInt(m1.toString());Objectm2=session.getAttribute(num2);intn2=Integer.parseInt(m2.toString());Objectm3=session.getAttribute(num3);intn3=Integer.parseInt(m3.toString());if(n1!=0){out.println(id1.toString()++价格为:+str1.toString()++购买数量:+m1.toString()+br);}else{out.println();}if(n2!=0){out.println(id2.toString()++价格为:+str2.toString()++购买数量:+m2.toString()+br);}else{out.println();};%p您需要支付的总金额为:%=g1*n1+g2*n2+g3*n3%元/p/body/html2.源代码文件:cookie.jsp,user.jsp,response.jspcookie.jsp%@pagelanguage=javacontentType=text/html;charset=UTF-8pageEncoding=UTF-8%!DOCTYPEhtmlPUBLIC-//W3C//DTDHTML4.01Transitional//EN=Content-Typecontent=text/html;charset=UTF-8titlecookie/title/headbodyformname=form1method=postaction=user.jsppalign=center用户名:inputtype=textname=username/ppalign=center密 码:inputtype=passwordname=password/ppalign=centerinputtype=submitname=submitvalue=提交  inputtype=resetname=canclevalue=取消/p/form/body/htmluser.jsp%@pagelanguage=javacontentType=text/html;charset=UTF-8pageEncoding=UTF-8%%request.setCharacterEncoding(gb2312);%!DOCTYPEhtmlPUBLIC-//W3C//DTDHTML4.01Transitional//EN=Content-Typecontent=text/html;charset=UTF-8titlecookie/title/headbody%Stringstrname=request.getParameter(username);Stringstrword=request.getParameter(password);CookienameCookie=newCookie(name,strname);nameCookie.setMaxAge(10*60);CookiewordCookie=newCookie(word,strword);wordCookie.setMaxAge(10*60);response.addCookie(nameCookie);response.addCookie(wordCookie);response.sendRedirect(response.jsp);%/body/htmlr

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功