zoukankan      html  css  js  c++  java
  • 模拟淘宝登录和购物车功能:使用cookie记录登录名,下次登录时能够记得上次的登录名,使用cookie模拟购物车功能,使用session记住登录信息并验证是否登录,防止利用url打开网站,并实现退出登录功能

    Login 




    <%@ 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[] cc=request.getCookies(); String cardid=""; if(cc!=null) { //遍历 for(Cookie c:cc) { 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"> <br> <input type="submit" value="登陆"> </form> </body> </html>
    check



    <%@ 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.print("成功"); response.setHeader("refresh", "3;url=Login.jsp"); } else { //验证 if(cardid.equals("cardid")&&password.equals("password")) { //用cookie记住卡号 Cookie cid= new Cookie("cardid",cardid); cid.setMaxAge(60*60); //设置cookie存在的时间 response.addCookie(cid); //保持登录状态 session.setAttribute("cardid", cardid); response.sendRedirect("Main.jsp"); } else { out.print("错误"); response.setHeader("refresh", "3;url=Login.jsp"); } } %> </body> </html>
    main



    <%@ 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> 淘宝首页 <br> <% Object obj=session.getAttribute("cardid"); if(obj==null) { out.print("会话超时或未登录"); response.setHeader("regfresh","3;url=Login.jsp"); } else { out.print(obj+"登录成功"); } %> <br> <br> <a href="Logout.jsp">退出登录</a> </body> </html>
    Logout


    <%@ 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>
  • 相关阅读:
    数据缓存/NSURLSession
    NSURLConnection基本使用/多线程断点下载/文件的上传
    HTTP协议/数据安全
    block的概念及基本使用 /block访问外部变量
    NSOperation简单介绍/NSOperation基本操作/自定义NSOperation
    GCD介绍/GCD的基本使用/GCD的常见用法
    NSThread方式创建线程/线程安全/线程间的通信
    面试常见知识点
    新课堂练习题
    线程概述
  • 原文地址:https://www.cnblogs.com/Levi1995/p/6018205.html
Copyright © 2011-2022 走看看