电脑桌面
添加小米粒文库到电脑桌面
安装后可以在桌面快捷访问

使用Servlet监听器统计在线用户VIP免费

使用Servlet监听器统计在线用户_第1页
1/4
使用Servlet监听器统计在线用户_第2页
2/4
使用Servlet监听器统计在线用户_第3页
3/4
使用Servlet监听器统计在线用户使用Servlet监听器可以统计在线用户,具体实现方法如下:1.通过ServletContext监听初始化一个application对象,保存在线用户列表;2.通过Session监听当用户登录成功设置Session属性时将用户名保存在列表中;3.通过Session监听当用户注销登录时将用户名从列表中删除。实例:OnlineListener.java:packagemgc.listener.test;importjava.util.*;importjavax.servlet.*;importjavax.servlet.http.*;publicclassOnlineListenerimplementsServletContextListener,ServletContextAttributeListener,HttpSessionListener,HttpSessionAttributeListener{privateServletContextapplication=null;privateHttpSessionsession=null;publicvoidcontextInitialized(ServletContextEventsce){//初始化一个application对象this.application=sce.getServletContext();//设置一个列表属性,用于保存在线用户名this.application.setAttribute("online",newArrayList());}publicvoidcontextDestroyed(ServletContextEventsce){}publicvoidattributeAdded(ServletContextAttributeEventscab){}publicvoidattributeRemoved(ServletContextAttributeEventscab){}publicvoidattributeReplaced(ServletContextAttributeEventscab){}publicvoidsessionCreated(HttpSessionEventse){}publicvoidsessionDestroyed(HttpSessionEventse){//取得用户名列表Listonline=(List)this.application.getAttribute("online");//取得当前用户名Stringusername=(String)se.getSession().getAttribute("username");//将此用户名从列表中删除online.remove(username);//将删除后的列表重新设置到application属性中this.application.setAttribute("online",online);}publicvoidattributeAdded(HttpSessionBindingEventse){//取得用户名列表Listonline=(List)this.application.getAttribute("online");//将当前用户名添加到列表中online.add(se.getValue());//将添加后的列表重新设置到application属性中this.application.setAttribute("online",online);}publicvoidattributeRemoved(HttpSessionBindingEventse){}publicvoidattributeReplaced(HttpSessionBindingEventse){}}web.xml:添加mgc.listener.test.OnlineListenersessionlistener.jsp:<%@pagecontentType="text/html;charset=GB2312"%><%@pageimport="java.util.*"%>sessionlistener用户名:注销<%Stringusername=request.getParameter("username");if(username!=null){session.setAttribute("username",username);}%>

在线用户:


<%Listonline=(List)getServletContext().getAttribute("online");Iteratoriter=online.iterator();while(iter.hasNext()){%>
  • <%=iter.next()%>
  • <%}%>logout.jsp:<%@pagecontentType="text/html;charset=GB2312"%>logout<%session.invalidate();response.setHeader("refresh","3;URL=onlinelistener.jsp");%>

    注销成功!

    3秒后自动返回登录页面
    如果没有跳转,请点这里<%@pagecontentType="text/html;charset=GB2312"%>logout<%session.invalidate();response.setHeader("refresh","3;URL=onlinelistener.jsp");%>

    注销成功!

    3秒后自动返回登录页面
    如果没有跳转,请点这里

    1、当您付费下载文档后,您只拥有了使用权限,并不意味着购买了版权,文档只能用于自身使用,不得用于其他商业用途(如 [转卖]进行直接盈利或[编辑后售卖]进行间接盈利)。
    2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。
    3、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。

    碎片内容

    使用Servlet监听器统计在线用户

    确认删除?
    VIP
    微信客服
    • 扫码咨询
    会员Q群
    • 会员专属群点击这里加入QQ群
    客服邮箱
    回到顶部