模拟淘宝登录和购物车功能:使用cookie记录登录名,下次登录时能够记得上次的登录名,使用cookie模拟购物车功能,使用session记住登录信息并验证是否登录,防止利用url打开网站,并实现退出登录功能
<%@ 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> <% //提取cookie保存到额卡号 Cookie[]cookies=request.getCookies(); String cardid=null; if(cookies!=null) { //遍历 for(Cookie c:cookies) { if(c.getName().equals("cardid")) { cardid=c.getValue(); } } } %> <form action="check.jsp" method="post"> 卡号:<input type="text"name="cardid" value="<%=cardid%>>"><br> 密码:<input type="password"name="password" style="width: 148px;"><br> <input type="submit"name="登陆"> </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 cardid=request.getParameter("cardid"); String password=request.getParameter("password"); if(cardid==null||password==null||cardid.equals("")||password.equals("")) { out.write("请正确登陆系统"); response.setHeader("refresh", "3;URL=login.jsp"); } else{ if(cardid.equals("1234")&&password.equals("123456")) { Cookie ckk=new Cookie("cardid",cardid); response.addCookie(ckk); ckk.setMaxAge(60*60*30*24); session.setAttribute("cardid", cardid); response.sendRedirect("main.jsp"); } else { out.print("卡号或密码不正确"); response.setHeader("refresh", "3;url=login.jsp"); } } %> </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> 淘宝首页 <% Object obj=session.getAttribute("cardid"); if(obj==null) { out.print("会话超时或未登录"); response.setHeader("refresh", "3; url=login.jsp"); } else { out.print(obj+",欢迎登陆"); } %> <br>
<br>
<form action="checkbox.jsp"method="post">
<input type="checkbox"name="name1"value="钢笔"/>钢笔<br>
<input type="checkbox"name="name2"value="鞋"/>鞋<br>
<input type="checkbox"name="name3"value="上衣"/>上衣<br>
<input type="submit"value="添加至购物车">
</form>
<a href="shopbox.jsp">查看购物车</a> <br> <a href="logout.jsp">退出登录</a> </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 session.invalidate(); out.print("退出成功"); response.setHeader("refresh", "2;url=login.jsp"); %> </body> </html>
<%@page import="java.net.URLDecoder"%> <%@page import="java.net.URLEncoder"%> <%@ 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> 您的购物车: <% Cookie[] mcc = request.getCookies(); //获取cookie集合 for(Cookie ok:mcc){ if(ok.getName().equals("name1")){ out.print(URLDecoder.decode(ok.getValue())+"<br>"); } } Cookie[] mcc1 = request.getCookies(); //获取cookie集合 for(Cookie ok1:mcc1){ if(ok1.getName().equals("name2")){ out.print(URLDecoder.decode(ok1.getValue())+"<br>"); } } Cookie[] mcc2 = request.getCookies(); //获取cookie集合 for(Cookie ok2:mcc2){ if(ok2.getName().equals("name3")){ out.print(URLDecoder.decode(ok2.getValue())+"<br>"); } } %> <br> </body> </html>
%@page import="java.net.URLEncoder"%> <%@ 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 name1=request.getParameter("name1"); String name2=request.getParameter("name2"); String name3=request.getParameter("name3"); if(name1!=null){ name1 =new String(request.getParameter("name1").getBytes("ISO-8859-1"),"UTF-8"); Cookie cks1= new Cookie("name1",URLEncoder.encode(name1)); response.addCookie(cks1); cks1.setMaxAge(60*60*24*30);}//设置过期时间 if(name2!=null){ name2 =new String(request.getParameter("name2").getBytes("ISO-8859-1"),"UTF-8"); Cookie cks2= new Cookie("name2",URLEncoder.encode(name2)); response.addCookie(cks2); cks2.setMaxAge(60*60*24*30);}//设置过期时间 if(name3!=null){ name3 =new String(request.getParameter("name3").getBytes("ISO-8859-1"),"UTF-8"); Cookie cks3= new Cookie("name3",URLEncoder.encode(name3)); response.addCookie(cks3); cks3.setMaxAge(60*60*24*30);}//设置过期时间 out.print("您的物品已经添加至购物车"); %> </body> </html>