✨✨个人主页:沫洺的主页
📚📚系列专栏: 📖 JavaWeb专栏📖 JavaSE专栏 📖 Java基础专栏📖vue3专栏
📖MyBatis专栏📖Spring专栏📖SpringMVC专栏📖SpringBoot专栏
📖Docker专栏📖Reids专栏📖MQ专栏📖SpringCloud专栏
💖💖如果文章对你有所帮助请留下三连✨✨
🌳Listener
🌲Listener介绍
概念:Listener 表示监听器,是 JavaWeb 三大组件(Servlet、Filter、Listener)之一。
- 监听器可以监听就是在application,session,request三个对象创建、销毁或者往其中添加修改删除属性时自动执行代码的功能组件
Listener分类:JavaWeb中提供了8个监听器
三个事件源(application,session,request)或者三个容器
第一类:监听容器本身的创建和销毁
- ServletContextListener
- HttpSessionListener
- ServletRequestListener
第二类:监听容器的内容变化
- ServletContextAttributeListener
- HttpSessionAttributeListener
- ServletRequestAttributeListener
第三类:专门针对Javabean的,让Javabean实现接口重写方法即可
- HttpSessionBindingListener
- HttpSessionActivationListener
🌾Listener使用
1. 定义类,实现ServletContextListener接口
2. 在类上添加@WebListener 注解
3.其他监听器的使用方法基本一致
package com.moming; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; //使用频率不高 //一般在spring框架初始化会使用到 public class ContextLoaderListener implements ServletContextListener { //初始化对象方法 @Override public void contextInitialized(ServletContextEvent servletContextEvent) { //加载资源, System.out.println("ContextLoaderListener..."); } //销毁方法 @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { //释放资源 } }
第三类:专门针对Javabean的使用方法
User实体类实现监听器
package com.moming.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; @Data @AllArgsConstructor @NoArgsConstructor public class User implements HttpSessionBindingListener { private int id; private String name; @Override public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) { System.out.println("[绑定]id为:"+id+"的用户:"+name+"被添加到session中"); } @Override public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) { System.out.println("[解绑定]id为:"+id+"的用户:"+name+"被移除session"); } }
AServlet
package com.moming.servlet; import com.moming.pojo.User; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; @WebServlet("/a") public class AServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //绑定Session User user = new User(110,"沫洺"); request.getSession().setAttribute("u", user); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
BServlet
package com.moming.servlet; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; @WebServlet("/b") public class BServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //解除绑定 request.getSession().removeAttribute("u"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
绑定
解绑定