java web monitor

Classification listener
accordance listening event listeners can be divided into the following three categories:

  1. Monitor their own domain object creation and destruction event listeners;
  2. Increase the listener domain object properties and remove event listeners;
  3. 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>
Published 108 original articles · won praise 46 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_44739706/article/details/104558232