第八周作业
1.登陆
输入用户名密码,判断用户名和密码相同,登陆成功,session中保存用户的用户名,进入主页main.jsp,主页有一个退出按钮,点击,回到登陆页login.jsp。要求:退出登录后,如果在浏览器直接输入主页main.jsp,访问不了,直接跳到登陆页、
2.购物车
和上一题一起,在main.jsp中做一个购物车,里面显示3个商品名和价格 每一个后面有一个加入购物车按钮,main.jsp中有一个按钮(或者超链接)可以显示购物车。(选作:在购物车中加删除按钮删除商品)
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title></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> <h1>登陆失败!</h1> </body> </html>
<%@ 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="login.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></title> </head> <body> <h1>退出成功!3秒后回到主页!</h1> <% session.invalidate(); response.setHeader("refresh", "3;url=login.jsp"); %> </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="logOut.jsp" method="post"> <input type="hidden" name="account" value="<%=account%>"> <input type="submit" value="查看我的物车"><br> </form> <h2>商品名:</h2> <form action="dddl.jsp" method="post" name="select"> 玉米(aa):¥5<input type="checkbox" name="toys" value="aa"><br> 西瓜(bb):¥20<input type="checkbox" name="toys" value="bb"><br> 苹果(cc):¥3<input type="checkbox" name="toys" value="cc"><br> <input type="button" value="加入购物车" onclick="inCart()"> </form> <a href="logSuccess.jsp"></a> <% }%> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登陆成功</title> </head> <body> <% 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>