the number of online users needs to be counted in the project, and the sessionCreated that uses HttpSessionListener,LoginController to call request.getSession (), HttpSessionListener event listeners is not executed.
related codes
@WebListener
public class SessionListener implements HttpSessionListener, HttpSessionAttributeListener {
public static final Logger logger = LoggerFactory.getLogger(SessionListener.class);
/**
* session
*/
public int count = 0;
public SessionListener(){
logger.info("SessionListener......");
}
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
logger.info("===============sessionCreated===============");
System.out.println("HttpSessionListenercountPP ");
countPP;
httpSessionEvent.getSession().getServletContext().setAttribute("count", count);
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
logger.info("===============sessionDestroyed===============");
System.out.println("HttpSessionListenercount-- ");
count--;
httpSessionEvent.getSession().getServletContext().setAttribute("count", count);
}
@Override
public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("1111111111111111111111111111");
}
@Override
public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("222222222222222222222222222222");
}
@Override
public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("333333333333333333333333333333333");
}
when the project starts, the printed SessionListener., indicates that the listener class has been initialized and the listener has been called.