Classification listener
accordance listening event listeners can be divided into the following three categories:
- Monitor their own domain object creation and destruction event listeners;
- Increase the listener domain object properties and remove event listeners;
- Listening to bind to an object in the domain of state HttpSession event listener
First, create a class: inheritance HttpSessionListener
package com.jbit.Listiener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
@WebListener(value="Myhttpsession")
public class Myhttpsession implements HttpSessionListener {
public static int peopleOnLine=0;
@Override
public void sessionCreated(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("创建回话监听");
peopleOnLine++;
arg0.getSession().setAttribute("people", peopleOnLine);
}
@Override
public void sessionDestroyed(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("销毁");
peopleOnLine--;
arg0.getSession().setAttribute("people", peopleOnLine);
}
}
Here is the index.jsp page
<body>
在线人数是:<%=session.getAttribute("people") %>
</body>