欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

servlet监听实现统计在线人数功能 附源码下载

程序员文章站 2024-02-29 15:30:52
本文实例为大家分享了servlet统计在线人数的具体代码,供大家参考,具体内容如下 servletcontext事件监听器---->针对applicationsco...

本文实例为大家分享了servlet统计在线人数的具体代码,供大家参考,具体内容如下

servletcontext事件监听器---->针对applicationscope
servletcontextlistener(*)
对整个web应用的装载和卸载进行监听。
 servletcontextattributelistener
对servletcontext中的信息存放、删除和替换进行监听。
servletcontext就是servlet上下文监听,在web中表示的是对启动服务和销毁服务进行监听,需要实现的接口:
servletcontextlistener接口,实现的就是对上下午进行监听:
void contextinitialized(servletcontextevent sce):启动上下文时的监听
void contextdestroyed(servletcontextevent sce):销毁上下文时进行的监听
除了对上下文的启动和销毁进行监听的之外,还可以对上下文的属性进行监听:servletcontextattributelistener接口。
void attributeadded(servletcontextattributeevent event):设置上下文属性监听
void attributeremoved(servletcontextattributeevent event):移除上下文属性的监听
void attributereplaced(servletcontextattributeevent event):修改上下文属性的监听
servletcontextattributeevent:事件,可以通过事件取得属性的内容和名称。
·取得属性名称:public java.lang.string getname()
·取得属性的值:public java.lang.object getvalue()
效果如下图:

当登录一个账号时

servlet监听实现统计在线人数功能 附源码下载

打开另一个浏览器,再登录一个账号

servlet监听实现统计在线人数功能 附源码下载

如上图,我们可以看到,程序已经完成了统计在线人数和显示人员列表的功能,那么他的实现流程是什么呢?

我们可以通过servletcontextlistener完成在线人数的统计和显示在线人数列表,首先listener和filter一样要在web.xml中进行描述。

代码如下:

<listener> 
 <listener-class>net.jvsun.listenertest</listener-class> 
</listener> 

为了测试这个程序,我们也必须完成用户登录功能。
数据库连接帮助类:

public class jdbchelper { 
 public static final string driver = "oracle.jdbc.driver.oracledriver"; 
 public static final string url = "jdbc:oracle:thin:@localhost:1521:xxx"; 
 public static final string dbname = "scott"; 
 public static final string password = "xxx"; 
 public static connection getconn() throws exception{ 
 class.forname(driver); 
 connection conn = drivermanager.getconnection(url, dbname, password); 
 return conn; 
 } 
} 

用户实体类:

public class userpojo implements serializable{ 
 private static final long serialversionuid = 7554548269035753256l; 
 private int id; 
 private string username; 
 private string password; 
 public int getid() { 
 return id; 
 } 
 public void setid(int id) { 
 this.id = id; 
 } 
 public string getusername() { 
 return username; 
 } 
 public void setusername(string username) { 
 this.username = username; 
 } 
 public string getpassword() { 
 return password; 
 } 
 public void setpassword(string password) { 
 this.password = password; 
 } 
 public userpojo(int id, string username, string password) { 
 super(); 
 this.id = id; 
 this.username = username; 
 this.password = password; 
 } 
 public userpojo(string username, string password) { 
 this.username = username; 
 this.password = password; 
 } 
 public userpojo() { 
 super(); 
 // todo auto-generated constructor stub 
 } 
 
} 

数据库处理类:

public class userdao { 
 public userpojo login(string username, string password) { 
 userpojo user=null; 
 connection conn = null; 
 preparedstatement pstate = null; 
 resultset res = null; 
 try { 
 conn=jdbchelper.getconn(); 
 string sql="select id,username from userinfo where username=? and password=?"; 
 pstate = conn.preparestatement(sql); 
 pstate.setstring(1, username); 
 pstate.setstring(2, password); 
 res = pstate.executequery(); 
 while(res.next()){ 
 user=new userpojo(res.getint(1),username,null); 
 } 
 } catch (exception e) { 
 e.printstacktrace(); 
 }finally{ 
 try { 
 res.close(); 
 pstate.close(); 
 conn.close(); 
 } catch (sqlexception e) { 
 e.printstacktrace(); 
 } 
 
 } 
 return user; 
 } 
} 

servlet类:

public class userservlet extends httpservlet{ 
 
 @override 
 protected void doget(httpservletrequest request, httpservletresponse response) 
 throws servletexception, ioexception { 
 string path = request.getcontextpath(); 
 response.setcontenttype("text/html; charset=utf-8"); 
 request.setcharacterencoding("utf-8"); 
 string username = request.getparameter("username"); 
 string password = request.getparameter("password"); 
 servletcontext application = this.getservletcontext();//取得application对象 
 list<string> list = (list<string>) application.getattribute("alluser"); 
 if(list.indexof(username) == -1){ 
 userpojo pojo = new userdao().login(username, password); 
 if(null != pojo){ 
 httpsession session = request.getsession(true);//取得session对象 
 session.setattribute("username", username); 
 } 
 
 } 
 response.sendredirect(path+"/index.jsp"); 
 } 
 
 @override 
 protected void dopost(httpservletrequest req, httpservletresponse resp) 
 throws servletexception, ioexception { 
 doget(req, resp); 
 } 
 
} 

监听类:

public class listenertest implements servletcontextlistener,httpsessionattributelistener,httpsessionlistener{ 
 servletcontext application = null; 
 public void contextdestroyed(servletcontextevent event) { 
 system.out.println("服务器关闭"); 
 } 
 
 public void contextinitialized(servletcontextevent event) { 
 list<string> list = new arraylist<string>(); 
 //用来保存所有已登录的用户 
 application = event.getservletcontext(); 
 //取得application对象 
 application.setattribute("alluser", list); 
 //将集合设置到application范围属性中去 
 
 } 
 
 public void attributeadded(httpsessionbindingevent se) { 
 list<string> list = (list<string>)application.getattribute("alluser"); 
 //假设:用户登陆成功之后,只将户名设置到session中 
 string username = (string)se.getvalue(); 
 //取得用户名 
 if(list.indexof(username) == -1){ 
 //表示此用户之前没有登陆 
 list.add(username); 
 application.setattribute("alluser", list); 
 } 
 } 
 
 public void attributeremoved(httpsessionbindingevent se) { 
 list<string> list = (list<string>)application.getattribute("alluser"); 
 list.remove((string)se.getvalue()); 
 application.setattribute("alluser", list); 
 } 
 
 public void attributereplaced(httpsessionbindingevent se) { 
 
 } 
 
 public void sessioncreated(httpsessionevent event) { 
 
 } 
 
 public void sessiondestroyed(httpsessionevent event) { 
 
 } 
} 

登录页面

<%@page contenttype="text/html; charset=utf-8"%> 
<%@page import="java.util.*" %> 
<% 
 string path = request.getcontextpath(); 
 %> 
 
<html> 
 <body> 
 
 <form action="<%=path %>/login" method="post"> 
 账号: 
 <input type="text" name="username" /> 
 <br /> 
 密码: 
 <input type="password" name="password" /> 
 <br /> 
 <input type="submit" value="提交" /> 
 </form> 
 
 </body> 
 
</html> 

显示在线人数和在线人员的列表界面

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> 
<% 
string path = request.getcontextpath(); 
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; 
%> 
 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> 
<html> 
 <head> 
 <base href="<%=basepath%>" rel="external nofollow" rel="external nofollow" > 
 <title></title> 
 </head> 
 
 <body> 
 <ul> 
 <% 
 system.out.println(application.getattribute("alluser")); 
 if(null != application.getattribute("alluser")){ 
 list<string> list = (list<string>)application.getattribute("alluser"); 
 %> 
 <h2>在线人数:<%=list.size() %></h2> 
 <% 
 for(string s:list){ 
 %> 
 <a>姓名:</a><%=s %><a>---->此时在线</a><br> 
 
 <% 
 } 
 } 
 %> 
 </ul> 
 <hr/> 
 <a href="<%=path %>/logout.jsp" rel="external nofollow" >注销</a> 
 </body> 
</html> 

注销界面:

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> 
<% 
string path = request.getcontextpath(); 
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; 
%> 
 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> 
<html> 
 <head> 
 <base href="<%=basepath%>" rel="external nofollow" rel="external nofollow" > 
 <title>logout</title> 
 </head> 
 <body> 
 <% 
 session.removeattribute("username"); 
 session.invalidate(); 
 response.sendredirect("login.jsp"); 
 %> 
 </body> 
</html> 

代码下载地址:servlet统计在线人数

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。