zoukankan      html  css  js  c++  java
  • cookie案例

     

    Summary:

    cookie是以字符串的形式保存数据的,把数据保存在客户端.

    一个cookie只能标识一种信息,它至少包含有一个标识该信息的名称(name)和设置值(value)

    setMaxAge(0)//可以删除cookie,用JavaScript也可以删除

    //常用的方法

    setMaxAge()//设置最大的有效期

    setValue()|getValue()  

    getName();

    setPath()|getPath()

     

    /**

     * cookie测试

     * @author 邵海雄

     * @date   2015-5-18  下午07:12:33

     */

    public class CookieServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

    doPost(request, response);

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {

    request.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;cookies!=null&& i < cookies.length; i++) {

    if (cookies[i].getName().equals("lastAccessTime")) {

    //得到用户上次的访问时间

    long cookieValue = Long.parseLong(cookies[i].getValue());

    Date date = new Date(cookieValue);

    out.print(DateFormat.getDateInstance().format(date));

    }

    }

    //给用户回送最新访问的时间

    Cookie cookie = new Cookie("lastAccessTime", System.currentTimeMillis()+"");

    //设置有效期

    cookie.setMaxAge(1*30*24*3600);//1*30*24*3600   这是一个月的时间

    //设置cookie保存的路径

    cookie.setPath("/Cookie");

    //添加Cookie

    response.addCookie(cookie);

    }

     

    }

  • 相关阅读:
    Kafka 消费者及消费者分区策略
    c++与c
    Exactly Once 语义
    如何在CentOS 8服务器上安装FreeIPA身份和授权解决方案?
    如何在Linux Mint 20上安装Wine
    如何在Ubuntu 20.04 LTS服务器上安装Wireguard
    如何在Ubuntu 20.04 LTS服务器上安装Apache JMeter
    如何在Linux服务器中使用SAR命令
    MongoDB是什么,它是如何工作的?
    如何在Ubuntu 20.04 LTS上安装SSH服务器
  • 原文地址:https://www.cnblogs.com/shaohaixiong/p/4513055.html
Copyright © 2011-2022 走看看