zoukankan      html  css  js  c++  java
  • 简单的Cooki案例——记录用户上次访问该网页的时间

    功能:

    帮助网站实现提示客户端计算机上次访问网站的时间

    实现原理:

    将每一个会话作为一次访问过程,将每次会话的开始时间作为每次访问网站的时间,然后将这个时间以Cookie的形式存储到客户端的计算机中,客户端进行下次访问时通过该Cookie回传上次访问站点的时间值。

    <%@ page import="java.util.Date" %><%--
      Created by IntelliJ IDEA.
      User: root
      Date: 2017/11/20 0020
      Time: 16:31
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <%
        //记录用户上次访问该页面的时间
        Cookie[] cookies = request.getCookies();
        Cookie timeCookie = null;
        if(cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                String cookieName = cookie.getName();
                if (cookieName.equals("time")) {
                   timeCookie = cookie;
                }
            }
        }
        if(timeCookie != null){
            String value = timeCookie.getValue();
            response.getWriter().print("用户上次访问该网页的时间是: " + value);
        }else {
            response.getWriter().print("用户第一次访问该网页");
        }
        timeCookie = new Cookie("time",(new Date()).toString());
    
        timeCookie.setMaxAge(999999);
    
        response.addCookie(timeCookie);
    
    %>
    </body>
    </html>
  • 相关阅读:
    WebStorm破解版
    React Native实战一
    Button加在UITableViewHeaderFooterView的self.contentView上导致不能响应点击
    centos7在vmware上无法上网
    重定向和转发的区别
    http和https的区别
    Runtime Error! R6025-pure virtual function call 问题怎么解决
    myeclipse部署web项目部署按钮无效
    Collections工具类的使用
    泛型集合
  • 原文地址:https://www.cnblogs.com/realshijing/p/7872735.html
Copyright © 2011-2022 走看看