signed

QiShunwang

“诚信为本、客户至上”

Cookie

2021/3/21 9:12:29   来源:

Cookie的使用及部分方法

package com.example.servlet03;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *
 *
 * cookie不能设置中文,如果设置中文,需要在设置时将中文使用URLEncoder.encode()方法进行编码
 * 获取时用urlDecoder.decode()方法解码
 *
 *
 * cookie.setPath("/");可设定在不同项目下查看cookie对象
 */

@WebServlet("/cook01")
public class Cookie01 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //得到cookie对象
        Cookie cookie = new Cookie("uname","李");

        //相应cookie对象
        resp.addCookie(cookie);

        //得到cookie对象
        Cookie cookie01 = new Cookie("uname01","王");

        //相应cookie对象
        resp.addCookie(cookie01);
    }
}

package com.example.servlet03;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * cookie的获取
 *      获取所有的cookie对象放回cookie数组
 *        Cookie[] arr = request.getCookies();
 */

@WebServlet("/cook02")
public class Cookie02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Cookie[] arr = req.getCookies();

        if(arr !=null && arr.length>0){
            for (Cookie cookie: arr) {
                System.out.println("名称:"+cookie.getName()+"  值:"+cookie.getValue());

                //获取指定值
                if ("uname".equals(cookie.getName())){
                    System.out.println("指定值为:"+cookie.getName()+"值:"+cookie.getValue());
                }
            }
        }

    }
}

package com.example.servlet03;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * cookie的到期时间
 *    设置cookie对象的失效时间
 *
 *    正整数:
 *        表示cookie会存活指定秒数
 *    负整数:
 *        默认 :-1
 *        表示cookie只在浏览器中存活,浏览器关闭则失效
 *    零:
 *        表示立即删除,删除cookie对象
 *
 */

@WebServlet("/cook03")
public class Cookie03 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

       //获取cookie对象
        Cookie cookie = new Cookie("name","君");
        //设置失效时间
        cookie.setMaxAge(-1);
        resp.addCookie(cookie);


        //获取cookie对象
        Cookie cookie1 = new Cookie("name1","xiang");
        //设置失效时间
        cookie.setMaxAge(20);
        resp.addCookie(cookie1);


        //获取cookie对象
        Cookie cookie2 = new Cookie("name2","ya");
        //设置失效时间
        cookie.setMaxAge(0);
        resp.addCookie(cookie2);

        //如果cookie存在,删除cookie

        Cookie cookie4= new Cookie("name1","null");
        cookie4.setMaxAge(0);
        resp.addCookie(cookie4);


    }
}