简介
通过session实现简易购物车。
session对象保存用户的登录信息以及用户的购物信息,使得用户在不同的页面购买商品后仍能读取幷显示之前所购买的商品。
- head.jsp:顶部列表界面,通过<%@ include file=“”%>包含在内
- fruit.jsp:水果购物界面
- drink.jsp:音频购物界面
- buy.jsp:添加到购物车界面,会转向到shop.jsp页面,为商品界面和cart.jsp界面传递用户选取的商品信息
- cart.jsp:购物车界面
- check.jsp:验证用户登录信息的页面,若没有登录,则不能访问,会自动跳转到login.html页面上
- logout.jsp:用户退出页面
login.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> * { padding: 0; margin: 0; } .main { margin-top: 5%; } .title { font-size: 30px; font-weight: 700; text-align: center; } .body { height: 110px; width: 400px; margin: 50px auto; text-align: center; } .user, .passwd { margin: auto; width: 60%; height: 35px; } .user .left, .passwd .left { float: left; margin-right: 10px; } .user .right, .passwd .right{ float: left; } .foot { position: relative; margin: auto; width: 100%; height: 20%; } .foot .sub { position: absolute; left: 125px; } .foot .reset { position: absolute; right: 115px; } .sub button, .reset button { width: 70px; height: 40px; } </style> </head> <body> <form action="login.jsp" method="post" class="main"> <div class="title">用户登录</div> <div class="body"> <div class="user"> <div class="left">用户:</div> <div class="right"> <input type="text" name="user"> </div> </div> <div class="passwd"> <div class="left">密码:</div> <div class="right"> <input type="password" name="passwd"> </div> </div> <div class="foot"> <div class="sub"> <button type="submit" name="submit">登录</button> </div> <div class="reset"> <button type="reset" name="reset">重置</button> </div> </div> </div> </form>
</body> </html>
|
login.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| <%-- Created by IntelliJ IDEA. User: DELL Date: 2022/9/16 Time: 23:58 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html> <head> <title>Title</title> <script> window.onload=function(){ var t=4; var span = document.querySelector("span"); var timer = setInterval(function(){ if(t===0) { clearInterval(timer); } else { span.innerHTML=t; t--; }
},1000) } </script> </head> <body> <% request.setCharacterEncoding("UTF-8"); String user = request.getParameter("user"); String passwd = request.getParameter("passwd"); if(user.length()==0 || passwd.length()==0){ out.println("<h3>请正确输入,<span>5</span>秒后返回登录<a href='login.html'>登录页面</a></h3>"); response.setHeader("Refresh","5;url=login.html"); } else if(user.equals("kittates")&&passwd.equals("kittates") && user.length()!=0 &&passwd.length()!=0) { session.setAttribute("loginUserName",user); response.sendRedirect("success.jsp?name="+user); }
else { out.println("<h3>用户名或密码错误,<span>5</span>秒后返回登录<a href='login.html'>登录页面</a></h3>"); response.setHeader("Refresh","5;url=login.html"); } %> </body> </html>
|
success.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <%-- Created by IntelliJ IDEA. User: DELL Date: 2022/9/17 Time: 0:10 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%--check一下是否用户登录--%> <%@ include file="check.jsp"%> <html> <head> <title>Title</title> <script> window.onload=function(){ var t=4; var span = document.querySelector("span"); var timer = setInterval(function(){ if(t===0) { clearInterval(timer); } else { span.innerHTML=t; t--; }
},1000) } </script> </head> <body> <%@ include file="head.jsp"%> <% out.println("<h3>欢迎光临 "+name+" 登录成功</h3>"); %> </body> </html>
|
head.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <%-- Created by IntelliJ IDEA. User: DELL Date: 2022/9/17 Time: 17:05 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <div align="center"> <a href="fruit.jsp">水果店</a> <a href="drink.jsp">饮品店</a> <a href="cart.jsp">购物车</a> <a href="logout.jsp">退出登录</a> <hr> </div> </body> </html>
|
check.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <%-- Created by IntelliJ IDEA. User: DELL Date: 2022/9/17 Time: 17:10 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <script> window.onload=function(){ var t=4; var span = document.querySelector("span"); var timer = setInterval(function(){ if(t===0) { clearInterval(timer); } else { span.innerHTML=t; t--; }
},1000) } </script> </head> <body> <% String name = (String)session.getAttribute("loginUserName"); if(name==null){ out.println("<h3>请先登录,<span>5</span>秒后返回登录<a href='login.html'>登录页面</a></h3>"); response.setHeader("Refresh","5;url=login.html"); return; }
%> </body> </html>
|
fruit.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <%-- Created by IntelliJ IDEA. User: DELL Date: 2022/9/17 Time: 17:21 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ include file="check.jsp"%> <html> <head> <title>Title</title> </head> <body> <%@include file="head.jsp"%> <div align="center"> <form action="buy.jsp" method="post"> <select name="goods" id=""> <option value="梨">梨</option> <option value="苹果">苹果</option> <option value="香蕉">香蕉</option> <option value="橘子">橘子</option> </select> <button type="submit" name="submit">加入购物车</button> </form> </div> </body> </html>
|
buy.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <%@ page import="java.util.ArrayList" %><%-- Created by IntelliJ IDEA. User: DELL Date: 2022/9/17 Time: 17:28 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ include file="check.jsp"%> <html> <head> <title>Title</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); String goods = request.getParameter("goods"); if(goods==null){ response.sendRedirect("success.jsp"); } else{ ArrayList<String> carts = (ArrayList<String>)session.getAttribute("carts"); if(carts == null){ carts = new ArrayList<String>(); } carts.add(goods); session.setAttribute("carts",carts); response.sendRedirect("cart.jsp"); } %> </body> </html>
|
drink.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <%-- Created by IntelliJ IDEA. User: DELL Date: 2022/9/17 Time: 17:24 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ include file="check.jsp"%> <html> <head> <title>Title</title> </head> <body> <%@include file="head.jsp"%> <div align="center"> <form action="buy.jsp" method="post"> <select name="goods" id=""> <option value="啤酒">啤酒</option> <option value="可乐">可乐</option> <option value="牛奶">牛奶</option> <option value="咖啡">咖啡</option> </select> <button type="submit">加入购物车</button> </form> </div> </body> </html>
|
cart.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <%@ page import="java.util.ArrayList" %><%-- Created by IntelliJ IDEA. User: DELL Date: 2022/9/17 Time: 17:35 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ include file="check.jsp"%> <html> <head> <title>Title</title> </head> <body> <%@include file="head.jsp"%> <div align="center"> <% ArrayList<String> carts = (ArrayList<String>)session.getAttribute("carts"); if(carts==null){ out.print("<h3>你的购物车为空</h3>"); } else{ out.println("您的购物车中选购了:"); for(String t:carts){ out.println(t+" "); } out.println(); } %> </div> </body> </html>
|
logout.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <%-- Created by IntelliJ IDEA. User: DELL Date: 2022/9/17 Time: 17:40 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page errorPage="pageError.jsp"%> <html> <head> <title>Title</title> <script> window.onload=function(){ var t=4; var span = document.querySelector("span"); var timer = setInterval(function(){ if(t===0) { clearInterval(timer); } else { span.innerHTML=t; t--; }
},1000) } </script> </head> <body> <% session.invalidate(); out.println("<h3>退出中,<span>5</span>秒后返回<a href='login.html'>登录页面</a></h3>"); response.setHeader("Refresh","5;url=login.html"); %> </body> </html>
|