项目中统计在线用户数量和同一用户只能登陆一次的需求,查询联系 HttpSessionBindingListener接口的使用,记录以备后用
下面为我的测试例子,首先建个web工程,例子中程序包括:OnLineUser.java ,login.jsp ,logout.jsp,onLineUser.jsp四个文件
OnLineUser.java清单:
package
pub;
/**/ /*
* onLineUser类实现HttpSessionBindingListener接口
* onLineUser类将具有HttpSessionBindingListener接口的特有属性
* 那么HttpSessionBindingListener接口的特有属性是什么呢?
* HttpSessionBindingListener接口具有的两个空函数
* public void valueBound(HttpSessionBindingEvent e)
* public void valueUnBound(HttpSessionBindingEvent e)
*
* onLineUser实现一些方法:获取在线用户数
*
*/
import javax.servlet.http. * ;
import java.util. * ;
public class OnLineUser implements HttpSessionBindingListener ... {
public OnLineUser()...{
}
private Vector users=new Vector();
public int getCount()...{
users.trimToSize();//调整Vector users的容量为Vector users的大小
return users.capacity();//返回users的容量
}
public boolean existUser(String userName)...{
users.trimToSize();
boolean existUser=false;
for (int i=0;i<users.capacity();i++ )
...{
if (userName.equals((String)users.get(i)))
...{
existUser=true;
break;
}
}
return existUser;
}
public boolean deleteUser(String userName) ...{
users.trimToSize();
if(existUser(userName))...{
int currUserIndex=-1;
for(int i=0;i<users.capacity();i++)...{
if(userName.equals((String)users.get(i)))...{
currUserIndex=i;
break;
}
}
if (currUserIndex!=-1)...{
users.remove(currUserIndex);
users.trimToSize();
return true;
}
}
return false;
}
public Vector getOnLineUser()
...{
return users;
}
public void valueBound(HttpSessionBindingEvent e) ...{
users.trimToSize();
System.out.println("请求:::::::::::"+e.getName());
if(!existUser(e.getName()))...{
users.add(e.getName());
System.out.print(e.getName()+" 登入到系统 "+(new Date()));
System.out.println(" 在线用户数为:"+getCount());
}else...{
System.out.println(e.getName()+"已经存在");
}
}
public void valueUnbound(HttpSessionBindingEvent e) ...{
users.trimToSize();
String userName=e.getName();
deleteUser(userName);
System.out.print(userName+" 退出系统 "+(new Date()));
System.out.println(" 在线用户数为:"+getCount());
}
}
/**/ /*
* onLineUser类实现HttpSessionBindingListener接口
* onLineUser类将具有HttpSessionBindingListener接口的特有属性
* 那么HttpSessionBindingListener接口的特有属性是什么呢?
* HttpSessionBindingListener接口具有的两个空函数
* public void valueBound(HttpSessionBindingEvent e)
* public void valueUnBound(HttpSessionBindingEvent e)
*
* onLineUser实现一些方法:获取在线用户数
*
*/
import javax.servlet.http. * ;
import java.util. * ;
public class OnLineUser implements HttpSessionBindingListener ... {
public OnLineUser()...{
}
private Vector users=new Vector();
public int getCount()...{
users.trimToSize();//调整Vector users的容量为Vector users的大小
return users.capacity();//返回users的容量
}
public boolean existUser(String userName)...{
users.trimToSize();
boolean existUser=false;
for (int i=0;i<users.capacity();i++ )
...{
if (userName.equals((String)users.get(i)))
...{
existUser=true;
break;
}
}
return existUser;
}
public boolean deleteUser(String userName) ...{
users.trimToSize();
if(existUser(userName))...{
int currUserIndex=-1;
for(int i=0;i<users.capacity();i++)...{
if(userName.equals((String)users.get(i)))...{
currUserIndex=i;
break;
}
}
if (currUserIndex!=-1)...{
users.remove(currUserIndex);
users.trimToSize();
return true;
}
}
return false;
}
public Vector getOnLineUser()
...{
return users;
}
public void valueBound(HttpSessionBindingEvent e) ...{
users.trimToSize();
System.out.println("请求:::::::::::"+e.getName());
if(!existUser(e.getName()))...{
users.add(e.getName());
System.out.print(e.getName()+" 登入到系统 "+(new Date()));
System.out.println(" 在线用户数为:"+getCount());
}else...{
System.out.println(e.getName()+"已经存在");
}
}
public void valueUnbound(HttpSessionBindingEvent e) ...{
users.trimToSize();
String userName=e.getName();
deleteUser(userName);
System.out.print(userName+" 退出系统 "+(new Date()));
System.out.println(" 在线用户数为:"+getCount());
}
}
==========================================================================
login.jsp 清单:
<%
...
@ page contentType="text/html;charset=gb2312"
%>
< html >
< head >
< title > 测试HttpSessionBindingListener接口 </ title >
</ head >
< body >
< form name ="login" method ="post" action ="onLineUser.jsp" >
< input type =text name ="username" >
< input type =submit name ="submit" value ="登陆" >
</ form >
</ body >
</ html >
< html >
< head >
< title > 测试HttpSessionBindingListener接口 </ title >
</ head >
< body >
< form name ="login" method ="post" action ="onLineUser.jsp" >
< input type =text name ="username" >
< input type =submit name ="submit" value ="登陆" >
</ form >
</ body >
</ html >
================================================================================
logout.jsp清单:
<%
...
@ page contentType="text/html;charset=gb2312"
%>
<% ... @ page import="pub.OnLineUser,java.util.*" %>
< jsp:useBean id ="onlineuser" class ="pub.OnLineUser" scope ="application" />
< html >
< head >
< title > 搞定JSP在线人数 </ title >
</ head >
< body >
< center >
< h1 > 登陆成功,欢迎您访问! </ h1 >
</ center >
<% ...
String username=request.getParameter("username");
if(username!=null&&onlineuser.deleteUser(username)){
out.println(username+"已经退出系统!");
session.removeAttribute(username);
out.println("<script>window.location="login.jsp";</script>");
}else{
out.println(username+"已经退出系统!");
out.println("<script>window.location="login.jsp";</script>");
}
%>
< center >
< p > elapsed制作 </ p >
< p > </ p >
< p >< a href ="logout.jsp" > 退出系统 </ a ></ p >
</ center >
</ body >
</ html >
<% ... @ page import="pub.OnLineUser,java.util.*" %>
< jsp:useBean id ="onlineuser" class ="pub.OnLineUser" scope ="application" />
< html >
< head >
< title > 搞定JSP在线人数 </ title >
</ head >
< body >
< center >
< h1 > 登陆成功,欢迎您访问! </ h1 >
</ center >
<% ...
String username=request.getParameter("username");
if(username!=null&&onlineuser.deleteUser(username)){
out.println(username+"已经退出系统!");
session.removeAttribute(username);
out.println("<script>window.location="login.jsp";</script>");
}else{
out.println(username+"已经退出系统!");
out.println("<script>window.location="login.jsp";</script>");
}
%>
< center >
< p > elapsed制作 </ p >
< p > </ p >
< p >< a href ="logout.jsp" > 退出系统 </ a ></ p >
</ center >
</ body >
</ html >
==============================================================================
onLineUser.jsp清单
<%
...
@ page contentType="text/html;charset=gb2312"
%>
<% ... @page import="java.util.*,pub.*" %>
< jsp:useBean id ="onlineuser" class ="pub.OnLineUser" scope ="application" />
< html >
< head >
< title > 搞定JSP在线人数 </ title >
</ head >
< body >
< center >
< h1 > 登陆成功,欢迎您访问! </ h1 &
<% ... @page import="java.util.*,pub.*" %>
< jsp:useBean id ="onlineuser" class ="pub.OnLineUser" scope ="application" />
< html >
< head >
< title > 搞定JSP在线人数 </ title >
</ head >
< body >
< center >
< h1 > 登陆成功,欢迎您访问! </ h1 &