2017.9.28 web设计简单的购物车应用案例--session的简单应用
该购物过程是在session范围内完成的,需要使用session对象实现信息的共享
(1)购买“肉类”商品的页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>购物肉类商品页面</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); if(request.getParameter("c1")!=null) session.setAttribute("s1",request.getParameter("c1")); if(request.getParameter("c2")!=null) session.setAttribute("s2",request.getParameter("c2")); if(request.getParameter("c3")!=null) session.setAttribute("c3",request.getParameter("c3")); %> 各种肉类大甩卖,一律十块:<br> <form method="post" action="ch03_18_buy1.jsp"> <p><input type="checkbox" name="c1" value="猪肉">猪肉 <input type="checkbox" name="c2" value="牛肉">牛肉 <input type="checkbox" name="c3" value="羊肉">羊肉 </p> <p><input type="submit" value="提交" name="B1"> <a href="ch03_18_buy2.jsp">买点别的</a> <a href="ch03_ 18_display.jsp">查看购物车</a> </p> </form> </body> </html>
(2)购买“球类”商品的页面
<%@page language="java" import="java.util.*" pageEncoding="UTF-8" %> <html> <head> <title>购买球类页面</title> </head> <body> <%request.setCharacterEncoding("UTF-8"); if(request.getParameter("b1")!=null) session.setAttribute("s4",request.getParameter("b1")); if(request.getParameter("b2")!=null) session.setAttribute("s5",request.getParameter("b2")); if(request.getParameter("b3")!=null) session.setAttribute("s6",request.getParameter("b3")); %> 各种球大甩卖,一律八块: <form method="post" action="ch03_18_buy2.jsp"> <p> <input type="checkbox" name="b1" value="篮球">篮球 <input type="checkbox" name="b2" value="足球">足球 <input type="checkbox" name="b3" value="排球">排球 </p> <p> <input type="submit" name="x1"value="提交"> <a href="ch03_18_buy1.jsp">买点别的</a> <a href="ch03_18_buy2.jsp">查看购物车</a> </form> </body> </html>
(3)显示购物车信息的页面
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>显示购物车购物信息</title> </head> <body> 你的选择结果是:<br> <%request.setCharacterEncoding("UTF-8"); String str=""; if(session.getAttribute("s1")!=null) { str=(String)session.getAttribute("s1"); out.println(str+"<br>"); } if(session.getAttribute("s2")!=null); { str=(String)session.getAttribute("s2"); out.print(str+"<br>"); } if(session.getAttribute("s3")!=null) { str=(String)session.getAttribute("s3"); out.print(str+"<br>"); } if(session.getAttribute("s4")!=null) { str=(String)session.getAttribute("s4"); out.print(str+"<br>"); } if(session.getAttribute("s5")!=null) { str=(String)session.getAttribute("s5"); out.print(str+"<br>"); } if(session.getAttribute("s6")!=null) { str=(String)session.getAttribute("s6"); out.print(str+"<br>"); } %> </body> </html>