A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王彪123 初级黑马   /  2019-1-10 14:05  /  1083 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

//1.获取所有Cookie
                        Cookie[] cookies = request.getCookies();
                        boolean flag = false;//没有cookie为lastTime
                        //2.遍历cookie数组
                        if(cookies != null && cookies.length > 0){
                            for (Cookie cookie : cookies) {
                                //3.获取cookie的名称
                                String name = cookie.getName();
                                //4.判断名称是否是:lastTime
                                if("lastTime".equals(name)){
                                    //有该Cookie,不是第一次访问
               
                                    flag = true;//有lastTime的cookie
               
                                    //设置Cookie的value
                                    //获取当前时间的字符串,重新设置Cookie的值,重新发送cookie
                                    Date date  = new Date();
                                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                                    String str_date = sdf.format(date);
                                    System.out.println("编码前:"+str_date);
                                    //URL编码
                                    str_date = URLEncoder.encode(str_date,"utf-8");
                                    System.out.println("编码后:"+str_date);
                                    cookie.setValue(str_date);
                                    //设置cookie的存活时间
                                    cookie.setMaxAge(60 * 60 * 24 * 30);//一个月
                                    response.addCookie(cookie);

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马