//javax.servlet包下监听接口: (或jakarta.servlet)
ServletContextListener //监听ServletContext对象的状态
ServletContextAttributeListener //监听ContextAttribute中数据状态
ServetRequestListener //监听ServetRequest的状态
ServletRequestAttributeListener //监听RequestAttribute中数据状态
//javax.servlet.http包下监听接口:
HttpSessionListener //监听HttpSession会话的状态
HttpSessionAttributeListener //监听session域中的数据状态
HttpSessionBindingListener (该监听器不需要配置xml或使用注解)//目标类实现该监听器,则监听该类是否有数据绑定
HttpSessionldListener //监听session的Id状态
HttpSessionActivationListener //监听session的存储状态(内存存储到硬盘, 硬盘到内存,又称钝化,活化)
监听器的使用步骤
所有的监听器中的方法,当指定时间发生后,则Web服务器调用该方法。
第一步:创建一个类,实现监听接口和接口方法。
public class MyListener implements ServletContextListener {
//ServletContext被创建时调用
@Override
public void contextInitialized(ServletContextEvent sce) {
//代码--------, Web服务器调用该方法。
}
// ServletContext被销毁时调用
@Override
public void contextDestroyed(ServletContextEvent sce) {
//代码--------, Web服务器调用该方法。
}
}
第二步:web.xm文件配置或使用注解
<listener>
<listener-class>com.javaweb.MyListener</listener-class>
</listener>
@WebListener
AttributeListener
@WebListener
public class AttributeListenerDemo implements HttpSessionAttributeListener {
//当向session域中存储数据的时,Web服务器执行该方法attributeAdded
@Override
public void attributeAdded(HttpSessionBindingEvent event) {
System.out.println("Add");
}
//当向session域中删除数据的时,Web服务器执行该方法attributeRemoved
@Override
public void attributeRemoved(HttpSessionBindingEvent event) {
System.out.println("Removed");
}
//当向session域中替换某一个数据的时,Web服务器执行该方法attributeReplaced
@Override
public void attributeReplaced(HttpSessionBindingEvent event) {
System.out.println("Replaced");
}
}
BindingListener
Student类
public class Student implements HttpSessionBindingListener {
//valueBound绑定数据
@Override
public void valueBound(HttpSessionBindingEvent event) {
System.out.println("valueBound");
}
//valueUnbound解绑数据
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
System.out.println("valueUnbound");
}
private int id;
private String name;
private int age;
public Student() {
}
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Servlet类
@WebServlet("/se/bind")
public class SessionBindingListenerDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取session对象
HttpSession session = request.getSession();
Student student =new Student();
student.setId(01);
student.setName("张三");
student.setAge(18);
session.setAttribute("student",student);
}
}
IdListener
@WebListener
public class HttpSessionldListenerDemo implements HttpSessionIdListener {
//当向session域中ID发生改变时,Web服务器执行该方法sessionIdChanged
@Override
public void sessionIdChanged(HttpSessionEvent httpSessionEvent, String s) {
}
}
ActivationListener
@WebListener
public class HttpSessionActivationListenerDemo implements HttpSessionActivationListener {
@Override
public void sessionWillPassivate(HttpSessionEvent se) {
//钝化
}
@Override
public void sessionDidActivate(HttpSessionEvent se) {
//活化
}
}
HttpSessionListener方式监听器统计网络在线人数
Web.xml
<!-- 设置session超时时长,2分钟-->
<session-config>
<session-timeout>2</session-timeout>
</session-config>
//此为演示案例,注意线程安全
@WebListener
public class HttpSessionListenerDemo implements HttpSessionListener {
public static long k=0;
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("sessionCreated");
k++;
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("sessionDestroyed");
k--;
}
}
HttpSessionBindingListener方式监听器统计网络在线人数
//此为演示案例,注意线程安全
User类
public class User implements HttpSessionBindingListener {
@Override
public void valueBound(HttpSessionBindingEvent event) {
//用户登入
ServletContext servletContext = event.getSession().getServletContext();
Object onLine = servletContext.getAttribute("onLine");
if (onLine == null){
servletContext.setAttribute("onLine",1);
}else {
Long count = (Long) onLine;
count++;
servletContext.setAttribute("onLine",count);
}
}
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
//用户退出
ServletContext servletContext = event.getSession().getServletContext();
Object onLine = servletContext.getAttribute("onLine");
Long count = (Long) onLine;
count++;
servletContext.setAttribute("onLine",count);
}
private String username;
private String password;
--有参无参构造方法 get和set方法--
}
Servlet类
//假设用户登入成功
User user =new User();
user.setUsername("张三");
session.setAttribute("user",user);
//假设用户登入失败 , 从session从删除User对象
session.removeAttribute("user");