JSP第八周作业
1.登陆 login.jsp
输入用户名密码,判断用户名和密码相同,登陆成功,session中保存用户的用户名,进入主页main.jsp,主页有一个退出按钮,点击,回到登陆页login.jsp。要求:退出登录后,如果在浏览器直接输入主页main.jsp,访问不了,直接跳到登陆页。
2.购物车
和上一题一起,在main.jsp中做一个商品展示,里面显示3个商品名和价格 ,每一个后面有一个加入购物车按钮,main.jsp中有一个按钮(或者超链接)可以显示购物车show.jsp 。(选作:在购物车show.jsp中加删除按钮删除商品)
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <script type="text/javascript"> function login() { if (formtext.caccount.value == "") { alert("账号不能为空!"); return; } if (formtext.cpassword.value == "") { alert("密码不能为空!"); return; } formtext.submit(); } </script> <form name="formtext" action="dologin.jsp" method="post"> <b>账号</b> <input type="text" name="caccount" /> <br> <b>密码</b> <input type="password" name="cpassword" /> <br> <input type="button" onclick="login()" value="登录" /> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% String uname = request.getParameter("caccount"); String upwd = request.getParameter("cpassword"); if (uname.equals(upwd)) { session.setAttribute("uname", uname); request.getRequestDispatcher("main.jsp").forward(request, response); } else { response.sendRedirect("login.jsp"); } %> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="po.Commodity"%> <%@ page import="java.util.List"%> <%@ page import="java.util.ArrayList"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% String uname = (String) session.getAttribute("uname"); if (uname == null) response.sendRedirect("login.jsp"); %> <form action="logout.jsp" method="post">      <input type="submit" value="退出"> </form> <br> <form action="shopping.jsp" method="post">      <input type="submit" value="显示购物车"> </form> <h3>     商品展示</h3> <ul> <form action="add.jsp"> <input type="hidden" name="cname" value="水浒传"> <input type="hidden" name="cprice" value="55"> <li>商品名:水浒传 价格:55 <input type="submit" value="加入购物车"></li> </form> <form action="add.jsp"> <input type="hidden" name="cname" value="三国志"> <input type="hidden" name="cprice" value="45"> <li>商品名:三国志 价格:45 <input type="submit" value="加入购物车"></li> </form> <form action="add.jsp"> <input type="hidden" name="cname" value="西游记"> <input type="hidden" name="cprice" value="35"> <li>商品名:西游记 价格:35 <input type="submit" value="加入购物车"></li> </form> </ul> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% session.invalidate(); response.setHeader("refresh", "1;url=login.jsp"); %> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="po.Commodity"%> <%@ page import="java.util.List"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <ul> <% List<Commodity> slist = (List<Commodity>) session.getAttribute("slist"); System.out.print("123"); System.out.print(slist.isEmpty()); System.out.print(slist == null); for (Commodity c : slist) { String cname = c.getCname(); double cprice = c.getCprice(); out.print("<form action='remove.jsp'>"); out.print("<input type='hidden' name='cname' value='" + cname + "'>"); out.print("<input type='hidden' name='cprice' value='" + cprice + "'>"); out.print("<li>商品名:" + cname + " 价格:" + cprice + " <input type='submit' value='删除'></li>"); out.print("</form>"); System.out.print("123"); } %> </ul> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="java.util.List"%> <%@ page import="po.Commodity"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <%!public Commodity search(List<Commodity> slist, String cname) { for (Commodity c : slist) { if (c.getCname().equals(cname)) { return c; } } return null; }%> <% String cname = request.getParameter("cname"); double cprice = Double.parseDouble(request.getParameter("cprice")); List<Commodity> slist = (List<Commodity>) session.getAttribute("slist"); slist.remove(search(slist, cname)); request.getRequestDispatcher("shopping.jsp").forward(request, response); %> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="po.Commodity"%> <%@ page import="java.util.List"%> <%@ page import="java.util.ArrayList"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% List<Commodity> slist = (List<Commodity>) session.getAttribute("slist"); if (slist == null) { slist = new ArrayList<>(); session.setAttribute("slist", slist); } String cname = request.getParameter("cname"); double cprice = Double.parseDouble(request.getParameter("cprice")); Commodity c = new Commodity(); c.setCname(cname); c.setCprice(cprice); slist.add(c); //session.setAttribute("slist", slist); request.getRequestDispatcher("main.jsp").forward(request, response); %> </body> </html>