zoukankan      html  css  js  c++  java
  • Cookie的简易用法

    Cookie

    Cookie是客户端技术,程序把每个用户的数据以cookie的形式写给用户各自的浏览器。当用户使用浏览器再去访问服务器中的web资源时,就会带着各自的数据去。这样,web资源处理的就是用户各自的数据了。

    1.在java中增加一个cookie

    Cookie cookie = new Cookie("mytest","123456");
    response.addCookie(cookie);

    2.在java中修改一个cookie

    Cookie[] cookies = request.getCookies();
    for(Cookie cookie : cookies) {
        if("mytest".equals(cookie.getName())) {
            cookie.setValue("mytestNEW");
            response.addCookie(cookie);
        }
    }

    3.怎么在java中删除一个cookie

    Cookie[] cookies = request.getCookies();
    for(Cookie cookie : cookies) {
        if("mytest".equals(cookie.getName())) {
            cookie.setMaxAge(0);
            response.addCookie(cookie);
        }
    }

    4.怎么在java中显示cookie列表

    Cookie[] cookies = request.getCookies();
    for(Cookie cookie : cookies) {
        try {
            response.getWriter().println(cookie.getName() + "-->" +   cookie.getValue());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    5.怎么在java中增加一个中文cookie

    Cookie cookie;
    try {
        cookie = new Cookie("mytest",URLEncoder.encode("我的测试", "UTF-8"));
        response.addCookie(cookie);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    6.怎么在java中显示中文cookie值

    Cookie[] cookies = request.getCookies();
    for(Cookie cookie : cookies) {
        if("mytest".equals(cookie.getName())) {
            try {      response.getWriter().println(URLDecoder.decode(cookie.getValue(), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    7.怎么在java中根据cookie名称获得cookie值

    Cookie[] cookies = request.getCookies();
    for(Cookie cookie : cookies) {
        if("mytest".equals(cookie.getName())) {
            try {
                response.getWriter().println(cookie.getName() + "-->" + cookie.getValue());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    8.怎么在java中设置cookie路径

    Cookie cookie1 = new Cookie("mytest","mytest1");
    cookie1.setPath("/test1");
    Cookie cookie2 = new Cookie("mytest","mytest2");
    cookie2.setPath("/test2");
    response.addCookie(cookie1);
    response.addCookie(cookie2);

    9.怎么在java中设置cookie过期时间为60秒

    Cookie cookie = new Cookie("mytest","mytest1");
    cookie.setMaxAge(60);
    response.addCookie(cookie);

    10.怎么在java中设置cookie域名

    Cookie cookie = new Cookie("mytest","mytest1");
    cookie.setDomain("127.0.0.1");
    response.addCookie(cookie);
  • 相关阅读:
    在vue项目中如何关闭Eslint校验
    npm安装依赖过程中报错:Error: Can't find Python executable "python", you can set the PYTHON env variable
    如何在SAE搭建属于自己的黑盒xss安全测试平台
    掌握下面常用函数,学php不再难
    解决ThinkPHP中开启调试模式无法加载模块的问题。
    生成规则:Q+年后两位+月+日卡券类型+八位字母和数字随机
    SPI的全名为Service Provider Interface
    03、第三个阶段,Java 核心技术
    02、第二个阶段,Java 基础入门
    01、第一个阶段,环境和工具准备
  • 原文地址:https://www.cnblogs.com/dandan1224/p/6015574.html
Copyright © 2011-2022 走看看