使用配置 xml配置
1.编写一个监听ServletContext生命周期的Listener;实现Listener接口在当前 2.项目中注册(在web.xml)使用;
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("this is contextInitialized");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("this is contextDestroyed");
}
}
<listener>
<listener-class>com.it.MyServletContextListener</listener-class>
</listener>
使用配置 SpringBoot 注解
1.在implements类上@WebListener
Listener改为实现ServletContextListener,还要在类上加@WebListener注解。其余可以不动:
2.SpringBoot启动类配置
只需要在SpringBoot 启动类中加上@ServletComponentScan
注解即可
@WebListener
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext servletContext = sce.getServletContext();
System.out.println("this is contextInitialized" + servletContext);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
ServletContext servletContext = sce.getServletContext();
System.out.println("this is contextDestroyed" + servletContext);
}
}
@ServletComponentScan
@SpringBootApplication
public class IntervienoteApplication {}
session生命周期
- 由于客户端与服务器之间的会话是无状态的机制,Session则可用于关联访问,因此多用与用户登录等功能上。
//引号中的"xxx"为页面中name标签里的值,逗号后面的是Servlet中的变量名。
request.getSession().setAttribute("xxx",xxx);//储存到session中
request.getSession().getAttribute("xxx",xxx);//读取session中的值
- 若要移除Session中特定的值,可以调用removeAttribute方法,但request中的SessionID仍不变,只是其中的值发生了改变,Session还是原来的Session
request.getSession().removeAttribute("xxx");
Attribute演示 HttpSessionAttributeListener
session.setAttribute( "user", "admin" );Thread.sleep( 3eee);
session. setAttribute( "user", "zhangsan");Thread.sleep( 3eee);
session.removeAttribute( "user");
@WebListener
public class MySession implements HttpSessionAttributeListener {
@Override
public void attributeAdded(HttpSessionBindingEvent se) {
String name = se.getName();//获取属性
Object value = se.getValue();//获取属性数值
System.out.println("session添加属性");
}
@Override
public void attributeRemoved(HttpSessionBindingEvent se) {
System.out.println("session移除属性");
}
@Override
public void attributeReplaced(HttpSessionBindingEvent se) {
System.out.println("session修改属性");
}
}
Session 钝化 活化 HttpSessionActivationListener
User user = new User();
httpSession.setAttribute("loginUser", user);
@Override
public void sessionDidActivate(HttpSessionEvent se) {
HttpSession session = se.getSession();//要钝化的session
System.out.println("user和session一起钝化了");
}
@Override
public void sessionWillPassivate(HttpSessionEvent se) {
System.out.println("user和session 活化来");
}
Session 綁定 解绑 HttpSessionBindingListener
@Override
User user = new User();
httpSession.setAttribute("loginUser", user);
httpSession.removeAttribute("loginUser");
// 检测user对象绑定 添加就是绑定
@Override
public void valueBound(HttpSessionBindingEvent event) {
HttpSession session = event.getSession();
Object value = event.getValue(); // 获取的绑定的数值
}
// 检测user解绑了 移除就是解绑
public void valueUnbound(HttpSessionBindingEvent event) {}
使用场景
ServletContextListener:监听服务器启动停止; HttpsessionBindingListener:监听某个对象绑定到session域中;
标签:session,void,Listener,println,Session,user,监听器,Override,public From: https://blog.51cto.com/u_15993308/6192449