码迷,mamicode.com
首页 > 其他好文 > 详细

servlet会话理解--cookie使用方法

时间:2017-04-11 11:16:16      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:会话   equal   max   ...   tco   for   time   cookie   res   

1.获取用户上次登陆的时间

Cookie cookies =request.getCookie();

boolean b=false;

if(cookies!=NULL){

  for(Cookie cookie:cookies){

    String name=cookie.getName();  

    if("lasttime".equals(name)){

      out.println("上次登录时间为:"+cookie.value());

      b=true; 

      SimpleDateFormat sdf=new SimpleDateFormat("...");

      String time=sdf.format(java.util.Date());

      cookie.setValue("lasttime", time);

      cookie.setMaxAge(3600*24*7);

      reponse.addCookie(cookie);  

      break;

    }

  }

}

if(!b){

  out.println("您是第一次登陆!");

  SimpleDateFormat sdf=new SimpleDateFormat("...");

  String time=sdf.format(java.util.Date());

  Cookie cookie=new Cookie("lasttime", time);

  cookie.setMaxAge(3600*24*7);

  response.addCookie(cookie);

  

}

servlet会话理解--cookie使用方法

标签:会话   equal   max   ...   tco   for   time   cookie   res   

原文地址:http://www.cnblogs.com/danielFF/p/6692532.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!