一 概念
session:会话对象,服务器为每一个浏览器创建一个session,且只有一个session
二 原理分析
使用浏览器第一次访问服务器时,服务器会创建一个session对象,session对象创建好了之后会把sessionId放在cookie中返回给(response)客户端。以后的每一次请求都会带着sessionId,来确定自己之前访问过服务器。
当两方有一方过期就要重新登录
三 使用
1)session的创建
//通过request获取sessionId,如果有,则在本地就创建过了session对象
//如果没有sessionId,或者过期了,就在本地创建一个session对象,
//并且返回
HttpSession sessoin=request.getSession();
//可以获取sessionId
System.out.println(session.geiId);
2)session的属性的设置和获取
//给session添加属性属性name: username,属性 value:TOM
session.setAttribute("username","TOM");
//添加属性 name: password; value: tommmm
session.setAttribute("password","tommmm");
//获取属性
session.getAttribute("username");
session.getAttribute("password");
3)session的有效时间的设置
//以秒为单位,即在没有活动30分钟后,session将失效
session.setMaxInactiveInterval(30*60);
springboot中设置:
#cookie的存活时间
server.session.cookie.max-age:cookie的存活时间
server.session.cookie.name
server.session.cookie.http-only
server.session.cookie.domain
server.session.cookie.path
server.session.cookie.secure
server.session.cookie.comment
server.session.timeout:session的超时时间
4)session的监听
实现接口HttpSessionListener
//当session创建时。
sessionCreated();
//当session被销毁或超时时。
sessionDestroyed();
//当给session添加属性时
实现接口HttpSessionAttributeListener
attributeAdded() ;
attributeRemoved();
attributeReplaced();
四 应用场景
环境
- springboot 2.1.0.RELEASE
- jdk 1.8
- ide:idea2019
1)统计当前访问本网站的人数
同一台电脑,不同的浏览器也会创建不同的session
监听类
package com.hwl.hwlspringboot.listener;
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.*;
import java.util.HashSet;
//监听器
@WebListener
public class SessionListener implements HttpSessionListener, HttpSessionAttributeListener {
@Override
public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("attributeRemoved");
HttpSession session = httpSessionBindingEvent.getSession();
System.out.println("key----:" + httpSessionBindingEvent.getName());
System.out.println("value---:" + httpSessionBindingEvent.getValue());
}
@Override
public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("attributeRemoved");
}
@Override
public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("attributeReplaced");
}
@Override
public void sessionCreated(HttpSessionEvent event) {
System.out.println("sessionCreated");
//获取当前的session
HttpSession session = event.getSession();
//通过session获取上下文
ServletContext application = session.getServletContext();
// 在application范围由一个HashSet集保存所有的session
HashSet sessions = (HashSet) application.getAttribute("sessions");
//如果之前没有创建该对象,就创建
if (sessions == null) {
sessions = new HashSet();
application.setAttribute("sessions", sessions);
}
// 新创建的session均添加到HashSet集中
sessions.add(session);
// 可以在别处从application范围中取出sessions集合
// 然后使用sessions.size()获取当前活动的session数
System.out.println("有人加入,当前人数为" + sessions.size());
}
@Override
public void sessionDestroyed(HttpSessionEvent event) throws ClassCastException {
HttpSession session = event.getSession();
System.out.println(session.getCreationTime());
System.out.println(session.getLastAccessedTime());
ServletContext application = session.getServletContext();
HashSet sessions = (HashSet) application.getAttribute("sessions");
// 销毁的session均从HashSet集中移除
sessions.remove(session);
System.out.println("sessionDestroyed");
System.out.println("有人退出,当前人数为" + sessions.size());
}
}