Cookie、Session(重点)的原理机制

会话 :用户打开一个浏览器,点击了很多超链接,访问多个 web 资源,关闭浏览器,这个过程可以称之 为会话;
有状态会话 :一个同学来过教室,下次再来教室,我们会知道这个同学,曾经来过,称之为有状态会 话;
一个网站,怎么证明你来过?
客户端 服务端
1. 服务端给客户端一个 信件,客户端下次访问服务端带上信件就可以了; cookie
2. 服务器登记你来过了,下次你来的时候我来匹配你; seesion

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

 


Cookie:

 

1. 从请求中拿到 cookie 信息
2. 服务器响应给客户端 cookie
cookie :一般会保存在本地的 用户目录下 appdata
package com.cc.hello;

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;

import static java.lang.System.out;

public class cookie 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");

        PrintWriter xx = resp.getWriter();
        Cookie[] cookies = req.getCookies();
    if (cookies!=null){
        xx.write("你你上一时间:");
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            if (cookie.getName().equals("lastLoginTime")){
                long l = Long.parseLong(cookie.getValue());
                Date date = new Date(l);
xx.write(date.toLocaleString());
            }
        }



    }else{
xx.write("这是第一次");
    }
        Cookie lastLoginTime = new Cookie("lastLoginTime", System.currentTimeMillis() + "");
    resp.addCookie(lastLoginTime);


    }

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

别忘了去xml配置,配置完成就运行一下可以得到下图:

一个网站 cookie 是否存在上限! 聊聊细节问题
  • 一个Cookie只能保存一个信息;
  • 一个web站点可以给浏览器发送多个cookie,最多存放20cookie
  • Cookie 大小有限制 4kb

 

  • 300个cookie浏览器上限

 

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

 

 

Session(重点):

 

 
什么是 Session
  • 服务器会给每一个用户(浏览器)创建一个Seesion对象;
  • 一个Seesion独占一个浏览器,只要浏览器没有关闭,这个Session就存在;
  • 用户登录之后,整个网站它都可以访问!--> 保存用户的信息;保存购物车的信息…..
Sessioncookie的区别:
  • Cookie是把用户的数据写给用户的浏览器,浏览器保存 (可以保存多个)
  • Session把用户的数据写到用户独占Session中,服务器端保存 (保存重要的信息,减少服务器资源的浪费)
  • Session对象由服务创建;
使用场景:
  • 保存一个登录用户的信息;
  • 购物车信息;
  • 在整个网站中经常会使用的数据,我们将它保存在Session中;
package com.kuang.servlet;
import com.kuang.pojo.Person;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
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;charset=utf-8");
//得到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>
<!--15分钟后Session自动失效,以分钟为单位-->
<session-timeout>15</session-timeout>
</session-config>

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值