第六次作业
1.登陆
输入用户名密码,判断用户名和密码相同,登陆成功,session中保存用户的用户名,进入主页main.jsp,
主页有一个退出按钮,点击,回到登陆页login.jsp。要求:退出登录后,如果在浏览器直接输入主页main.jsp,
访问不了,直接跳到登陆页。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <script type="text/javascript"> function v() { if (loginForm.uname.value=="") { alert("账号不能为空!"); return; } if (loginForm.password.value=="") { alert("密码不能为空!"); return; } loginForm.submit(); } </script> <body> <form name="loginForm" action="m.jsp" method="post"> 账号:<input type="text" name="uname"><br> 密码:<input type="password" name="password"> <br> <input type="submit" value="登录" onclick="v()"> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <% request.setCharacterEncoding("utf-8"); String uname = request.getParameter("uname"); String password = request.getParameter("password"); if (uname.equals(password)) { session.setAttribute("uname", uname); request.getRequestDispatcher("J.jsp").forward(request, response); } else { response.sendRedirect("q.jsp"); } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <% String uname=(String)session.getAttribute("uname"); //如果他是空,说明没登陆,直接访问该页面了 if(uname==null) response.sendRedirect("index.jsp"); %> 欢迎<br> <a href="n.jsp">退出登录</a> <br> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <%String src=request.getParameter("id"); %> <body> 登陆失败,请重试 <% response.setHeader("refresh", "5;url=index.jsp"); %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <% session.invalidate(); response.setHeader("refresh", "5;url=index.jsp"); %> 退出成功! </body> </html>
2.购物车
和上一题一起,在main.jsp中做一个购物车,里面显示3个商品名和价格 每一个后面有一个加入购物车按钮,
main.jsp中有一个按钮(或者超链接)可以显示购物车。(选作:在购物车中加删除按钮删除商品)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <script type="text/javascript"> function v() { if (loginForm.uname.value=="") { alert("账号不能为空!"); return; } if (loginForm.password.value=="") { alert("密码不能为空!"); return; } loginForm.submit(); } </script> <body> <form name="loginForm" action="m.jsp" method="post"> 账号:<input type="text" name="uname"><br> 密码:<input type="password" name="password"> <br> <input type="submit" value="登录" onclick="v()"> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <% request.setCharacterEncoding("utf-8"); String uname = request.getParameter("uname"); String password = request.getParameter("password"); if (uname.equals(password)) { session.setAttribute("uname", uname); request.getRequestDispatcher("J.jsp").forward(request, response); } else { response.sendRedirect("q.jsp"); } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <% String uname=(String)session.getAttribute("uname"); //如果他是空,说明没登陆,直接访问该页面了 if(uname==null) response.sendRedirect("index.jsp"); %> <h3>Magic的世界</h3> <form action="7.jsp"> <hr> 是否加入购物车?<br> <input name="item" type="checkbox" value="vip"> 高级会员 298 <br> <input name="item" type="checkbox" value="card"> 日落天台 价格随样式决定 <br> <input name="item" type="checkbox" value="calendar"> 新年台历 128<br> <input name="item" type="checkbox" value="pb"> 少年 梦境之物 89<br> <input name="item" type="checkbox" value="card book"> 官方纪念卡册 99 <br> <hr> <input type="submit" value="加入购物车"> </form> <a href="n.jsp">退出登录</a> <br> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> 购物车:<br> <% request.setCharacterEncoding("utf-8"); String itemName[]=request.getParameterValues("item"); if(itemName==null){ out.print("购物车空空如也"); } else{ for(int l=0;l<itemName.length;l++){ out.print("("+(l+1)+")"+itemName[l]+"<br>"); } } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <%String src=request.getParameter("id"); %> <body> 登陆失败,请重试 <% response.setHeader("refresh", "5;url=index.jsp"); %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <% session.invalidate(); response.setHeader("refresh", "5;url=index.jsp"); %> 退出成功! </body> </html>