public class HellServlet implements HttpSessionListener{@Overridepublic void sessionCreated(HttpSessionEvent arg0) {// TODO Auto-generated method stubSystem.out.println("HttpSession被建立");ServletContext application=arg0.getSession().getServletContext();Integer count = (Integer)application.getAttribute("count");if(count==null){count=1;application.setAttribute("count", count);}else{count++;application.setAttribute("count", count);}}@Overridepublic void sessionDestroyed(HttpSessionEvent arg0) {// TODO Auto-generated method stubSystem.out.println("HttpSession被销毁");ServletContext application=arg0.getSession().getServletContext();Integer count = (Integer)application.getAttribute("count");count--;application.setAttribute("count", count);}}//配置web.xml<listener><listener-class>com.kgc.listener.HellServlet </listener-class> </listener>
jsp 页面
<%Integer count=(Integer)application.getAttribute("count");out.println(“当前在线人数:”+count);%>
<--下面等价getAttribute()-->
当前在线用户人数:${applicationScope.count}人