您的位置:首页 > 其它

Servlet的在线人数监听

2013-11-02 00:00 375 查看
监听:OnlineUserList.java
package xiong.listener ;
import java.util.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
public class OnlineUserList implements ServletContextListener,HttpSessionAttributeListener,HttpSessionListener {
private ServletContext qq = null ;
public void contextInitialized(ServletContextEvent sce){
this.qq = sce.getServletContext() ;
this.qq.setAttribute("online",new TreeSet()) ;
}
public void contextDestroyed(ServletContextEvent sce){
}
public void attributeAdded(HttpSessionBindingEvent se){
Set all = (Set) this.qq.getAttribute("online") ;
all.add(se.getValue()) ;
this.qq.setAttribute("online",all) ;
}
public void attributeRemoved(HttpSessionBindingEvent se){
Set all = (Set) this.qq.getAttribute("online") ;
all.remove(se.getSession().getAttribute("userid")) ;
this.qq.setAttribute("online",all) ;
}
public void attributeReplaced(HttpSessionBindingEvent se){}
public void sessionCreated(HttpSessionEvent se){}
public void sessionDestroyed(HttpSessionEvent se){
Set all = (Set) this.qq.getAttribute("online") ;
all.remove(se.getSession().getAttribute("userid")) ;
this.qq.setAttribute("online",all) ;
}
}

登录页:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>www.xiongsheng.com, 熊胜的主页</title></head>
<body>
<form action="login.jsp" method="post">
用户ID:<input type="text" name="userid">
<input type="submit" value="登陆">
</form>
<%
String userid = request.getParameter("userid") ;
if(!(userid==null || "".equals(userid))){
session.setAttribute("userid",userid) ;
response.sendRedirect("list.jsp") ;
}
%>
</body>
</html>
显示页面:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title>www.xiongsheng.com, 熊胜的主页</title></head>
<body>
<%
Set all = (Set) this.getServletContext().getAttribute("online") ;
Iterator iter = all.iterator() ;
while(iter.hasNext()){
%>
<h3><%=iter.next()%></h3>
<%
}
%>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐