Cookie和Session

一、Cookie
1. 创建Cookie
复制代码
 /**
     * 创建一个新的Cookie可同时创建多个
     * @author: panther
     * @date: 2022/6/9 10:31
     * @param: [req, resp]
     * @return: void
     **/
    protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.创建cookie对象
        Cookie cookie = new Cookie("key","val");

        resp.setContentType("text/html,CharSet=UTF-8");

        //2. 通知客户端保存Cookie
        resp.addCookie(cookie);

        resp.getWriter().write("cookie创建成功");
    }
复制代码

2. 查找Cookie数组

 

复制代码
    /**
     * 获取Cookie和获取特定的cookie
     * @author: panther
     * @date: 2022/6/9 10:31
     * @param: [req, resp]
     * @return: void
     **/
    protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();

        for(Cookie cookie : cookies ){
            resp.getWriter().write("cookies["+cookie.getName()+","+cookie.getValue()+"] <br/>");
        }
    }
复制代码

 

3. 修改Cookie的值

复制代码
  /**
     * 修改Cookie的值
     * @author: panther
     * @date: 2022/6/9 10:32
     * @param: [req, resp]
     * @return: void
     **/
    protected void UpdateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Cookie cookie = new Cookie("key","newValue");
        resp.addCookie(cookie);
        resp.getWriter().write("Cookie修改成功");

        //方法二
        Cookie cookie1 = FindCookie.find("key", req.getCookies());
        if(cookie1 != null){
            cookie1.setValue("newValue2");
        }
        resp.getWriter().write("Cookie修改成功");
    }
复制代码

4.设置Cookie时长

复制代码
 /**
     * 设置Cookie的存活时长
     * @author: panther
     * @date: 2022/6/9 23:59
     * @param: [req, resp]
     * @return: void
     **/
    protected void SetLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Cookie cookie = new Cookie("path","path");
        cookie.setMaxAge(60*60);//一小时后超时
        cookie.setMaxAge(-1);//关闭页面后失效
    }
复制代码

二、Session

1. 创建Session

复制代码
   /**
     * 创建session
     * @author: panther
     * @date: 2022/6/9 16:15
     * @param: [req, resp]
     * @return: void
     **/
    protected void createSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //创建和获取会话对象
        HttpSession session = req.getSession();
        //判断客户端是否是新创建的session
        boolean isNew = session.isNew();

        //获取session的唯一标识
        String id = session.getId();
        resp.getWriter().write("是否是新创建的Session"+isNew+"<br>");
        resp.getWriter().write("id="+id);
    }
复制代码

2. 设置Seesion域值

复制代码
/**
     * 设置Session的域值
     * @author: panther
     * @date: 2022/6/9 16:45
     * @param: [req, resp]
     * @return: void
     **/
    protected void SetAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().setAttribute("key1","value1");
        System.out.println("set success");
    }
    /**
     * 得到Session域中的数据
     * @author: panther
     * @date: 2022/6/9 16:45
     * @param: [request, response]
     * @return: void
     **/
    protected void getAttribute(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        Object key1 = request.getSession().getAttribute("key1");
        response.getWriter().write("date is "+ key1);

    }
复制代码

3. 设置Session超时时长

复制代码
/**
     * Session的默认超时时长,可以通过配置在Web.xml文件中修改
     * @author: panther
     * @date: 2022/6/9 23:12
     * @param: [request, response]
     * @return: void
     **/
    protected void defaultLife(HttpServletRequest request, HttpServletResponse response) throws IOException {

        int maxInactiveInterval = request.getSession().getMaxInactiveInterval();

        response.getWriter().write("Session 默认存活时长"+maxInactiveInterval+"秒");
  
      HttpSession session = request.getSession();
        session.setMaxInactiveInterval(30);//设置超时时长为30秒设置负数永不超时
        response.getWriter().write("30秒后超时");

     HttpSession session = request.getSession();

        session.invalidate();//使当前会话立即超时
        response.getWriter().write("Session 超时结束");

    }
复制代码

 

posted @   panther125  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示