JSP第六次作业
1.登陆 login.jsp
输入用户名密码,判断用户名和密码相同,登陆成功,session中保存用户的用户名,进入主页main.jsp,主页有一个退出按钮,点击,回到登陆页login.jsp。要求:退出登录后,如果在浏览器直接输入主页main.jsp,访问不了,直接跳到登陆页
//代码见第二题
2.购物车
和上一题一起,在main.jsp中做一个商品展示,里面显示3个商品名和价格 ,每一个后面有一个加入购物车按钮,main.jsp中有一个按钮(或者超链接)可以显示购物车show.jsp 。(选作:在购物车show.jsp中加删除按钮删除商品)
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录</title> </head> <body> <script type="text/javascript"> function check() { if (login.account.value==""){ alert("请输入账号!"); return; }else if(login.psw.value=="") { alert("请输入密码!"); return; } login.submit(); } </script> <form action="check.jsp" method="post" name="login"> 账号:<input type="text" name="account"><br> 密码:<input type="password" name="psw"><br> <input type="checkbox" value="注册会员">注册会员 <input type="checkbox" value="不注册会员">不注册会员<br> <input type="button" value="登录" onclick="check()"> </form> <% %> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>check</title> </head> <body> <% String account=request.getParameter("account"); String psw=request.getParameter("psw"); if (psw.equals(account)){ session.setAttribute("account",account); request.getRequestDispatcher("logSuccess.jsp").forward(request,response); }else { request.getRequestDispatcher("logFail.jsp").forward(request,response); } %> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登陆成功</title> </head> <body> <script type="text/javascript"> function inCart() { alert("加入购物车成功!") <% String[] toyName=request.getParameterValues("toys"); session.setAttribute("toyName",toyName); %> select.submit(); } </script> <% String account =(String) session.getAttribute("account"); if (account == null) { out.print("请登录!3秒后跳转"); response.setHeader("refresh", "3;url=login.jsp"); } else {%> <h1>登陆成功!</h1> <h1>欢迎!<%=session.getAttribute("account")%> </h1> <h2>已成为会员!</h2><br> <form action="user.jsp" method="post"> <input type="hidden" name="account" value="<%=account%>"> <input type="submit" value="查看我都购物车"><br> </form> <h2>商品名:</h2> <form action="logSuccess.jsp" method="post" name="select"> 迪迦(tiga):¥999<input type="checkbox" name="toys" value="tiga"><br> 赛文(Severn):¥998<input type="checkbox" name="toys" value="Severn"><br> 小怪兽(littleMonster):¥1<input type="checkbox" name="toys" value="littleMonster"><br> <input type="button" value="加入购物车" onclick="inCart()"> </form> <a href="logOut.jsp">退出登录</a> <% }%> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登陆失败</title> </head> <body> <h1>登陆失败!</h1> </body> </html>
<% String account=request.getParameter("account"); %> <h1>欢迎!</h1> <h2> 您的账号:<%=account%><br> 购物车:<br> <% request.setCharacterEncoding("utf-8"); String[] toy=(String[]) session.getAttribute("toyName"); if (toy==null){ out.print("购物车空空如也!"); }else { out.println(Arrays.toString(toy)); } %> </h2> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>退出登录</title> </head> <body> <h1>退出成功!3秒后回到主页!</h1> <% session.invalidate(); response.setHeader("refresh", "3;url=login.jsp"); %> </body> </html>