jsp第八周作业
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> <title>登录界面 index.jsp</title> </head> <body> <form action="pan.jsp" name="form1"> 用户名:<input type="text" name="name"><br> <br> 密码:<input type="password" value="12345" name="password"><br> <br> <input type="button" value="登录" name="d" onclick="fu()"> </form> <script type="text/javascript"> function fu() { if (form1.name.value == "") { alert("账号不能为空"); return; } if (form1.password.value == "") { alert("密码不能为空"); return; } form1.submit(); } </script> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>界面pan.jsp</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); String password = request.getParameter("password"); if (name.equals(password)) { session.setAttribute("name", name); request.getRequestDispatcher("y.jsp") .forward(request, response); } else { request.getRequestDispatcher("no.jsp").forward(request, response); } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>登录成功界面y.jsp</title> </head> <body> <% String name = (String) session.getAttribute("name"); if (name == null) { response.sendRedirect("index.jsp"); } %> 欢迎你<%=name%><br> <br> <form action="tui.jsp"> <input type="submit" value="退出登录"> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>登录失败界面no.jsp</title> </head> <body>登录失败! </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>退出界面tui.jsp</title> </head> <body> <% session.invalidate(); response.setHeader("refresh", "0;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> <title>主界面y.jsp</title> </head> <body> <% String name = (String) session.getAttribute("name"); if (name == null) { response.sendRedirect("index.jsp"); } %> 欢迎你<%=name%><br> <br> <form action="buy.jsp" method="post"> 杯子 30元 <input type="checkbox" value="杯子 30元" name="buy">加入购物车<br><br> 眼影 99元 <input type="checkbox" value="眼影 99元" name="buy">加入购物车<br><br> 相册 50元 <input type="checkbox" value="相册 50元" name="buy">加入购物车<br><br> <input type="submit" value="查看购物车"> </form> <form action="tui.jsp"> <input type="submit" value="退出登录"> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>购物车界面buy.jsp</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String buy[] = request.getParameterValues("buy"); if (buy == null) { out.print("购物车为空"); } else { out.print("购买的商品为" + "<br/>"); for (int i = 0; i < buy.length; i++) { out.print("(" + (i + 1) + ")" + buy[i] + "<br/>"); } } %> <a href="y.jsp">返回</a> </body> </html>