zoukankan      html  css  js  c++  java
  • cookie在后台的存取

    /************存***********/
    //获取cookie值
    String cookieValue = "";
    Cookie[] cookies = request.getCookies();
    if (cookies != null) { //防止出现nullpoint错误
    for (Cookie cookie : cookies) {
    if (cookie.getName().equals("myCookie")) {
    cookieValue = cookie.getValue();
    }
    }
    }
    //更新cookie的值(id为新的存储到cookie里面的值)
    cookieValue = cookieValue + id.toString() + "%";
    Cookie userCookie = new Cookie("myCookie", cookieValue);
    userCookie.setMaxAge(30 * 24 * 60 * 60); //存活期为一个月 30*24*60*60
    userCookie.setPath("/");
    response.addCookie(userCookie);
    /************存***********/

    /************取***********/
    //再次获取cookie的值
    //此处已经获取到存在cookie中的id值,现拆分并从数据库中查询对应对象的集合
    String[] cookId = cookieValue.substring(0, cookieValue.length() - 1).split("%");
    /************取***********/

    controller参数里面需要HttpServletResponse response、HttpServletRequest request

    response存,request取

  • 相关阅读:
    C语言与水仙花数
    C语言break和continue
    C语言中常量
    C语言中计算变量占用内存空间
    C语言中if语句
    JavaScript放置位置区别
    单选按钮中实现点击文字选中
    C语言(4)
    C语言(3)
    【第四课】kaggle案例分析四
  • 原文地址:https://www.cnblogs.com/zhangyong0908/p/9339979.html
Copyright © 2011-2022 走看看