建议网站购物——建立一个简易购物网站,包括登录页面、商品选择页面和结账页面。

 //login.jsp
1
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%> 2 <html> 3 <head> 4 <title>简易购物网站</title> 5 </head> 6 7 <body> 8 <center><h2>登录页面</h2></center> 9 <p>输入您的个人信息: 10 <form action="shop.jsp" method="post"name=form> 11 <input type="text"name="user" ><br><br> 12 <input type="radio" name="Kind" value="普通会员"checked=true>普通会员 13 <input type="radio" name="Kind" value="VIP">VIP(享有九折优惠)<br><br> 14 <input type="submit" value="进入商店" name="submit"> 15 </form> 16 </body> 17 </html>
//shop.jsp
1
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%> 2 <html> 3 <head> 4 <title>简易购物网站</title> 5 </head> 6 7 <body> 8 <center><h2>商品选择页面</h2></center> 9 <% 10 request.setCharacterEncoding("UTF-8"); 11 String s=request.getParameter("user"); 12 session.setAttribute("name", s); 13 String k=request.getParameter("Kind"); 14 session.setAttribute("Kind", k); 15 out.println("欢迎"+k+":"+s+"来到本网站购物!"); 16 %> 17 <p>请选择要购买的商品: 18 <form action="account.jsp" method="post"name=form> 19 <input type="checkbox"name="Goods" value="风衣">风衣500 20 <input type="checkbox"name="Goods" value="牛仔裤">牛仔裤400 21 <input type="checkbox"name="Goods" value="运动鞋">运动鞋600<br><br> 22 <input type="submit" value="去结账" name="submit"> 23 </form> 24 </body> 25 </html>
//account.jsp
1
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%> 2 <html> 3 <head> 4 <title>简易购物网站</title> 5 </head> 6 7 <body> 8 <center><h2>结账页面</h2></center> 9 <% 10 request.setCharacterEncoding("UTF-8"); 11 String inst[]=request.getParameterValues("Goods"); 12 session.setAttribute("goodsN", inst.length); 13 for(int i=0;i<inst.length;i++) 14 { 15 session.setAttribute("goods"+i, inst[i]); 16 } 17 String customerKind=(String)session.getAttribute("kind"); 18 String customerName=(String)session.getAttribute("name"); 19 Double sum=0.0; 20 String customerGoods=""; 21 String Info=""; 22 int num=Integer.parseInt(String.valueOf(session.getAttribute("goodsN"))); 23 for(int i=0;i<num;i++) 24 { 25 customerGoods+=session.getAttribute("goods"+i)+""; 26 if(String.valueOf(session.getAttribute("goods"+i)).equals("风衣")); 27 sum+=500; 28 if 29 (String.valueOf(session.getAttribute("goods"+i)).equals("牛仔裤")) 30 sum+=400; 31 else 32 sum+=600; 33 } 34 if(customerKind.equals("VIP")) 35 { 36 sum=sum*0.9; 37 Info="您享有九折优惠,打折后的总金额是:"; 38 } 39 else 40 Info="总金额是:"; 41 42 %> 43 <p><%=customerKind %>的姓名是:<%=customerName %> 44 <p>选择购买的商品是<%=customerGoods%> 45 <p><%=Info %> 46 <p>欢迎您下次光临! 47 48 </body> 49 </html>

 

posted on 2017-11-07 22:36  小星_log  阅读(1427)  评论(0编辑  收藏  举报