首页 > 其他分享 >Cookie、Session

Cookie、Session

时间:2022-11-18 19:56:48浏览次数:34  
标签:浏览器 Session resp session cookie Cookie

会话

  • 会话:用户打开一个浏览器,点击了很多超链接,访问多个web资源,关闭浏览器,这个过程可以称之为会话
  • 有状态会话:你能怎么证明你是学生?
    • 录取通知书
    • 学校登记
  • 一个网站,怎么证明你来过?
  • 客户端 、 服务端
    • 服务端给客户端一个信件,客户端下次访问服务端带上信件就可以了;cookie
    • 服务器登记你来过了,下次你来的时候我来匹配你;session

保存会话的两种技术

  • cookie
    • 客户端技术(响应,请求)
  • session
    • 服务器技术,利用这个技术,可以保存用户的会话信息?我们可以把信息或者数据放在Session中!
  • 常见场景:网站登录之后,你下次不用再登录了,第二次访问直接就上去了!
  • 从请求中拿到cookie信息
  • 服务器响应给客户端cookie
Cookie[] cookies = req.getCookies();//获得cookie
cookie.getName();//获得cookie中的key
cookie.getValue();//获得cookie中的value
new Cookie("lastLoginTime", System.currentTimeMillis()+"");//新键一个cookie
cookie.setMaxAge(24*60*60);//设置cookie的有效期
resp.addCookie(cookie);//响应给客户端一个cookie
  • cookie:一般会保存在本地的用户目录下appdata
  • 一个网站cookie是否存在上限!(细节问题)
    • 一个Cookie只能保存一个信息
    • 一个web站点可以给浏览器发送多个cookie,最多存放20个cookie
    • Cookie大小有限制4kb
    • 浏览器上限300个cookie
  • 删除Cookie
    • 不设置有效期,关闭浏览器,自动失效
    • 设置有效期时间为0
  • 编码解码:
URLEncoder.encode("小淞","utf-8")
URLDecoder.decode(cookie.getValue(),"utf-8")

Session(重点)

什么是Session

  • 服务器会给每一个用户(浏览器)创建一个Session对象
  • 一个Session独占一个浏览器,只要浏览器没有关闭,这个Session就存在
  • 用户登录之后整个网站它都可以访问啊-->保存用户的信息;保存购物车的信息
  • Session和Cookie的区别
    • Cookie是把用户的数据写给用户的浏览器,浏览器保存(可以保存多个)
    • Session把用户的数据写到用户独占Session中,服务器端保存(保存重要的信息,减少服务器资源的浪费)
    • Session对象由服务器创建
  • 使用场景:
    • 保存一个登录用户的信息
    • 购物车信息
    • 在整个网站中经常会使用的数据,我们将它保存在Session中
  • 使用Session
public class SessionDemo01 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");

        //得到Session
        HttpSession session = req.getSession();
        //给Session中存东西
        session.setAttribute("name",new Person("小淞",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创建的时候做了什么事情
        //Cookie cookie = new Cookie("JSESSIONID",sessionId);
        //resp.addCookie(cookie);

    }

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

//得到Session
HttpSession session = req.getSession();

Person person = (Person) session.getAttribute("name");

System.out.println(person.toString());

HttpSession session = req.getSession();
session.removeAttribute("name");
//手动注销Session
session.invalidate();
  • 会话自动过期:web.xml配置
<!--设置Session默认的失效时间-->
<session-config>
    <!--1分钟后Session自动失效,以分钟为单位-->
    <session-timeout>1</session-timeout>
</session-config>

标签:浏览器,Session,resp,session,cookie,Cookie
From: https://www.cnblogs.com/song-hua/p/16903783.html

相关文章