Cookie和Session

Cookie and Session

会话

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

有状态会话:一个服务端证明客户端来过?

  1. 服务端给客户端一个信件,客户端下次访问服务端时带上信件就可以了;cookie
  2. 服务器登记你来过了,下次你来的时候我来匹配你。session

保存会话的两种技术

  1. cookie
  • 客户端技术(相应,请求)
  1. session
  • 服务器技术,利用这个技术,可以保存用户的会话信息。我们把信息和数据放在Session中。

常见场景:登录网站之后,无需重复登录。

Cookie

  1. 从请求中拿到cookie信息
  2. 服务器相应给客户端cookie
package com.guhuo.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class CookieDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 服务器,告诉你,你来的时间,把这个时间封装成为一个信件,你下次带来,我就知道你来的时间

        // 解决中文乱码
        req.setCharacterEncoding("utf-16");
        resp.setCharacterEncoding("utf-16");

        PrintWriter out = resp.getWriter();

        // Cookie,从服务器端从客户端获取
        Cookie[] cookies = req.getCookies(); //这里返回一个数组,说明可能会有多个Cookies

        // 判断Cookie是否存在
        if (cookies != null) {
            // 如果存在?
            out.write("你上一次访问的时间是:");

            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                // 获取cookie的名字
                String name = cookie.getName();
                if(name.equals("lastLoginTime")){
                    //获取cookie的值
                    String value = cookie.getValue();
                    long lastLoginTime = Long.parseLong(value);
                    Date data = new Date(lastLoginTime);
                    out.write(data.toLocaleString());
                }
            }
        } else {
            out.write("这是你第一次访问本网站"); // 第一次访问该网站时,没有Cookie
        }
        // 服务给客户端相应一个cookie:
        Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis()+"");
        cookie.setMaxAge(24*60*60); // 有效期为1天

        resp.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}

cookie:一般会保存在本地的用户目录的appdata下。

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

删除cookie:

  • 不设置有效期,关闭浏览器,自动失效;
  • 设置有效期时间为0;

Session

什么是Session:

  • 服务器会给每个用户(浏览器)创建一个Session对象
  • 一个Session独占一个浏览器,只要浏览器没有关闭,这个Session就存在
  • 用户登录之后,整个网站它都可以访问

Session和Cookie之间的差别

  • Cookie是把用户的数据写给用户的浏览器,浏览器保存(可以保存多个)
  • Session把用户的数据写到用户独占Session中,服务器端保存(保存重要的信息,减少服务器资源的浪费)
  • Session对象由服务器创建

使用场景:

  • 保存一个登录用户的信息;
  • 购物车信息;
  • 在整个网站中经常会使用的数据,我们将它保存在Session中;

Session的使用

package com.guhuo.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class SessionDemo extends HttpServlet {
    @Override
    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", "guhuo");
        session.setAttribute("name", new Preson("guhuo", 1));
        // 获取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.removeAttribute("name");
        session.invalidate();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

}

会话自动过期,可在web.xml中设置:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值