0
点赞
收藏
分享

微信扫一扫

JavaWeb_Cookie、Session

時小白 2022-05-01 阅读 125

Cookie、Session

Cookie、Session

1. 会话

  • 会话:用户打开了一个浏览器,点击了很多超链接,访问多个web资源,关闭浏览器,这个过程可以称之为会话

  • 有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾经来过,称之为有状态会话

    • 发票 商家给你发票
    • 登记 商家标记你来过
    • 网站如何证明你来过(服务端如何证明客户端来过)
      • 服务端给客户端一个信件,客户端下次访问服务端带上信件就可以了;cookie
      • 服务器登记你来过了,下次你来的时候我来匹配你;session

2. 保存会话的两种技术

  • cookie
    • 客户端技术(响应,请求)
  • session
    • 服务器技术,利用这个技术,可以保存用户的会话信息。我们可以把信息或者数据放在Session中
  • 常见例子:网站登录之后,你下次不用再登录了,第二次访问直接就上去了

3. Cookie

  • 从请求中拿到Cookie信息

  • 服务器响应给客户端Cookie

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //服务器告诉你,你来的时间,把这个时间封装成为一个信件,你带下来,我就知道你来了
    
            //解决中文乱码
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
    
            PrintWriter out = resp.getWriter();
    
            //Cookie,服务器端从客户端获取
            Cookie[] cookies = req.getCookies();//这里返回数组,说明Cookie可能存在多个
    
            //判断Cookie是否存在
            if (cookies != null){
                //如果存在怎么办
                out.write("你上一次访问的时间是:");
    
                for (int i = 0; i < cookies.length; i++) {
                    Cookie cookie = cookies[i];
                    //获取cookie的名字
                    if (cookie.getName().equals("LastLoginTime")){
                        //获取cookie中的值
                        long lastLoginTime = Long.parseLong(cookie.getValue());
                        Date date = new Date(lastLoginTime);
                        out.write(date.toLocaleString());
                    }
    
                }
    
            }else{
                out.write("这是您第一次访问本站");
            }
    
            //服务器给客户端响应一个cookie
            Cookie cookie = new Cookie("LastLoginTime", System.currentTimeMillis() + "");
            resp.addCookie(cookie);
    
        }
    
  • 一个网站的Cookie是否存在上限

    • 一个Cookie只能保存一个信息
    • 一个web站点可以给浏览器发送多个Cookie,最多存放20个
    • Cookie大小有限制4kb
    • 300个Cookie浏览器上线
  • 删除Cookie

    • 不设置有效期,关闭浏览器,自动失效
    • 设置有效期为0
  • 编码解码

    URLEncoder.encode("赵岩","utf-8")
       URLDecoder.decode(cookie.getValue(),"utf-8")
    

4. Session(重点)

  • 什么是Session

    • 服务器会给每一个用户(浏览器)创建一个Session对象
    • 一个Session独占一个浏览器,只要浏览器没关,这个Session就存在
    • 用户登录后,整个网站它都可以访问 -->保存用户信息
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
            //解决乱码问题
            req.setCharacterEncoding("utf-8");
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
    
            //得到Session
            HttpSession session = req.getSession();
    
            //给Session中存东西
            session.setAttribute("name",new Person("赵岩",19));
    
            //获取Session的ID
            String sessionId = session.getId();
    
            //判断是否是新的Session
            if (session.isNew()){
                resp.getWriter().write("session创建成功,ID:" + sessionId);
            }else {
                resp.getWriter().write("session已经在服务器中存在了,ID:" + sessionId);
            }
    
            //Session创建的时候做了什么,将Session放入Cookie中
    //        Cookie cookie = new Cookie("JSESSIONID",sessionId);
    //        resp.addCookie(cookie);
    
        }
    
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
            //解决乱码问题
            req.setCharacterEncoding("utf-8");
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
    
            //得到Session
            HttpSession session = req.getSession();
    
            Person  person = (Person) session.getAttribute("name");
    
            System.out.println(person.toString());
    
        }
    
  • 会话过期

    • 手动注销
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            HttpSession session = req.getSession();
            session.removeAttribute("name");
            //手动注销Session
            session.invalidate();
        }
    
    • 自动过期:web.xml配置
    <!--设置Session默认失效时间-->
        <session-config>
        <!--1min后Session自动失效,以分钟为单位-->
            <session-timeout>1</session-timeout>
        </session-config>
    
  • Session和Cookie的区别

    • Cookie是把用户的数据写给用户的浏览器,浏览器保存(可以保存多个)
    • Session是把用户的数据写到用户独占的Session中,服务器保存(保存重要信息,减少服务器资源浪费)
    • Session对象由服务器创建
    • Session使用场景
      • 保存一个登陆用户的信息
      • 在整个网站中经常会使用的数据,将它保存到Session中

笔记

JavaWeb_Cookie、Session.md百度网盘链接
提取码: ennv

举报

相关推荐

0 条评论