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>
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>
相关文章推荐
- Servlet中的监听器及监听在线人数实例
- Servlet监听案例之统计在线人数
- HttpSessionListener,ServletContextListener, ServletContextAttributeListener,ServletContainer监听在线人数
- servlet监听完成统计在线人数,显示在线人员列表(附源码)
- Servlet中的监听器及监听在线人数实例
- java 监听器(监听用户监听器也叫Listener,是Servlet的监听器,它可以监听客户端的请求、服务端的操作等。通过监听器,可 以自动激发一些操作,比如监听在线的用户的数量。当增加一在线人数)
- Servlet的监听器监听在线人数功能
- Servlet监听------------例子:统计当前在线人数
- servlet监听实现统计在线人数功能 附源码下载
- servlet监听完成统计在线人数,显示在线人员列表(附源码)
- [j2ee]servlet中的listener介绍,并用HttpSessionListener和HttpSessionBindingListener实现了在线人数列表
- java在线人数监听(没有好好整理,只是从别人那把帖子借鉴过来)
- Servlet-监听器获得当前在线人数
- (转)Servlet监听器统计在线人数
- java—实现一个监听器HttpServletRequest的创建销毁、在线人数 (56)
- Servlet中实现用servletContext对象统计网站在线人数
- JavaWeb学习记录(二十六)——在线人数统计HttpSessionListener监听实现
- Servlet监听器——实现在线登录人数统计小例子
- JSP&Servlet 统计在线人数及信息
- servlet+java+jsp实现在线人数的统计