2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 如何通过监听器统计人数?

如何通过监听器统计人数?

时间:2019-01-14 18:50:39

相关推荐

如何通过监听器统计人数?

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}人

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。