会话 Cookie和Session

会话

概述
会话表示从浏览器打开某个网站,在这个网站中无论操作了什么,直到关闭浏览器,这么一个过程,成为一个会话
那怎样才算会话结束?
客户端关闭了,或者服务器销毁了
为什么要处理会话:
长期保持会话,无论用户关闭少次浏览器,这个会话都要存在;
比如说:
你昨天来看了我的博客,我今天需要直到你昨天来看过
那么 1、你留下了一个标记,让我知道你来过
  2、你直接告诉我你来这边,我记录你来了
你就相当于客户端,我相当于服务器

Javaweb中针对这两个不同的端,诞生了两个小机制
cookie: 在客户端留下一点东西,客户端下次带过来,我就知道你来过了

Session: 在服务器端登记你来过。

Cookie

构造器:Cookie cookie = new Cookie(String name,String value);
服务器响应cookie给客户端:Response.addCookie(Cookie);
服务查看用户带来的请求是否有cookie:服务器查看用户带来的请求是否有cookie

cookie测试用户是否来过
@WebServlet(name = "Test02Servlet")
public class Test02Servlet extends HttpServlet {
    boolean flag = false;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //规定编码格式
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //检查请求的人是否带了Cookie
        Cookie[] cookies = request.getCookies();
        System.out.println("cookie" + cookies);

        if (flag) {//是否来过
            if (cookies != null) {//你是否有cookie
                for (int i = 0; i < cookies.length; i++) {
                    Cookie cookie = cookies[i];
                    if (cookie.getName().equals("lastLoginTime")) {
                        response.getWriter().print("你上一次来的时间为:" + cookie.getValue());
                        System.out.println("刷新了cookie");
                    }
                }
            }
        } else {
            response.getWriter().print("你第一次来,给你一个小饼干吧");
            System.out.println("给该用户一个cookie");
        }
        //建立一个cookie
        Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis() + "");
        //把这个cookie发给客户端
        response.addCookie(cookie);

        flag = true;
    }
}

第一次打开
在这里插入图片描述
之后再打开
在这里插入图片描述

Session

只要客户端一旦连接上服务器,服务器就会自动产生Session,session可以在一个会话中传递信息。通过setAttribute和getAttribute设置值和获取值。
由服务器控制,服务器如果重启了,信息就会丢失。

首先创建一个servlet1

@WebServlet(name = "SessionTest01")
public class SessionTest01 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //规定编码格式
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //HttpSession 得到的session对象
        HttpSession session = request.getSession();
        //得到sessionID,一次会话,一个sessionID
        String id = session.getId();
        response.getWriter().print("获得的sessionId:\n"+id+"\n");

        String name = "anye";
        //向session中存入一个值
        session.setAttribute("name",name);
        response.getWriter().print("存入信息成功"+name);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request,response);
    }
}

然后创建一个servlet2

@WebServlet(name = "SessionTest02")
public class SessionTest02 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //规定编码格式
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        //获得
        HttpSession session = request.getSession();
        String sessionId = session.getId();
        System.out.println("得到的SessionID"+sessionId);

        String name = (String)session.getAttribute("name");
        response.getWriter().print("得到的session存入的信息"+name);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

然后运行
在这里插入图片描述
在这里插入图片描述
销毁会话
方法一:session.invalidate();
方法二:

<!--session-config可以设置会话自动过期,时间分钟为单位-->
<session-config>
    <session-timeout>1</session-timeout>
</session-config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值