cookie实现上一次访问时间

package details;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * cookie保存用户上次访问时间
 * @author YH
 *
 */
public class CookieDemo extends HttpServlet {
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setCharacterEncoding("UTF-8");
  response.setContentType("text/html;charset=UTF-8");
  PrintWriter out = response.getWriter();
  out.print("您上次访问时间是:");
  //获取用户cookie,判断是否存在上次访问时间,若有就取得。如果是第一次访问就不存在上次访问时间。
  Cookie[] cookies = request.getCookies();
  for(int i=0; i<cookies.length && cookies!=null; i++){
   if(cookies[i].getName().equals("lastAccessTime")){
    Long cookieValue = Long.parseLong(cookies[i].getValue());
    Date date = new Date(cookieValue);
    out.print(date.toLocaleString());
   }
  }
  //更新上次访问时间到cookie中,也就是覆盖lastAccessTime
  Cookie cookie = new Cookie("lastAccessTime",System.currentTimeMillis()+"");
  cookie.setPath("/javaweb");   //设置有效路径为当前web应用
  cookie.setMaxAge(30*24*3600); //这里是秒单位
  response.addCookie(cookie);
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doGet(request,response);
 }

}

对于浏览商品的历史记录,也就是把商品的唯一ID以某种特定方式的字符串组合,保存到cookie中,例如:Cookie cookie = new Cookie("viewHistory","1-2-3-4-5");

取得该cookie的时候,对字符串进行split("-"),取得所有浏览商品历史的商品ID。通过ID调用service层的方法,获取商品,显示到浏览器上。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的自动登录并显示上一次登录时间cookie程序的示例: ```python import datetime import http.cookies import os import sys def get_last_login_time(): # 从 cookie 中获取上一次登录时间 cookie_str = os.environ.get('HTTP_COOKIE') if cookie_str: cookie = http.cookies.SimpleCookie() cookie.load(cookie_str) if 'last_login_time' in cookie: last_login_time = cookie['last_login_time'].value return datetime.datetime.strptime(last_login_time, '%Y-%m-%d %H:%M:%S') return None def set_last_login_time(): # 设置当前时间为登录时间,并将登录时间保存到 cookie 中 now = datetime.datetime.now() cookie = http.cookies.SimpleCookie() cookie['last_login_time'] = now.strftime('%Y-%m-%d %H:%M:%S') print(cookie.output()) def main(): # 获取上一次登录时间并显示 last_login_time = get_last_login_time() if last_login_time: print('上一次登录时间为:{}'.format(last_login_time)) else: print('这是您第一次登录') # 设置当前时间为登录时间,并将登录时间保存到 cookie 中 set_last_login_time() if __name__ == '__main__': main() ``` 该程序的实现方式是通过读取 HTTP 请求的 cookie 头部来获取上一次登录时间,并在登录成功后将当前时间保存到 cookie 中。程序中使用了 Python 的 `http.cookies.SimpleCookie` 类来处理 cookie,该类可以方便地将 cookie 转换为字符串,并且可以通过属性或方法来访问 cookie 中的键值对。需要注意的是,由于 cookie 可能会被篡改或伪造,因此在实际应用中需要进行相应的安全措施来保护 cookie 的安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值