package list;
import javax.servlet.http.*;
import javax.servlet.*;
import java.util.*;
public class onLineUser implements HttpSessionBindingListener {
public onLineUser(){
}
private Vector users=new Vector();
public int getCount(){
users.trimToSize();
return users.capacity();
}
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();
if(!existUser(e.getName())){
users.add(e.getName());
System.out.print(e.getName()+"\t 登入到系统\t"+(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+"\t 退出系统\t"+(new Date()));
System.out.println(" 在线用户数为:"+getCount());
}
}
jsp里
<jsp:useBean id="onlineuser" class="list.onLineUser" scope="application"/>
<% session = request.getSession(false); %>
<%
String username=(String)session.getAttribute("ID");
if (onlineuser.existUser(username)){
}else{
session.setMaxInactiveInterval(1200); //file://Sesion有效时长,以秒为单位
session.setAttribute(username,onlineuser);
}
out.println("在线人数:"+onlineuser.getCount()+"<br/>");
Vector vt=onlineuser.getOnLineUser();
Enumeration e = vt.elements();
out.println("在线用户列表");
while(e.hasMoreElements()){
out.println((String)e.nextElement());
}
%>
在被直接关闭的页面中
<body οnunlοad="if(window.screenLeft>=10000&&window.screenTop>=10000){window.open('./userexit.jsp','','width=50,height=30')}">
userexit.jsp页面处理将session失效
import javax.servlet.http.*;
import javax.servlet.*;
import java.util.*;
public class onLineUser implements HttpSessionBindingListener {
public onLineUser(){
}
private Vector users=new Vector();
public int getCount(){
users.trimToSize();
return users.capacity();
}
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();
if(!existUser(e.getName())){
users.add(e.getName());
System.out.print(e.getName()+"\t 登入到系统\t"+(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+"\t 退出系统\t"+(new Date()));
System.out.println(" 在线用户数为:"+getCount());
}
}
jsp里
<jsp:useBean id="onlineuser" class="list.onLineUser" scope="application"/>
<% session = request.getSession(false); %>
<%
String username=(String)session.getAttribute("ID");
if (onlineuser.existUser(username)){
}else{
session.setMaxInactiveInterval(1200); //file://Sesion有效时长,以秒为单位
session.setAttribute(username,onlineuser);
}
out.println("在线人数:"+onlineuser.getCount()+"<br/>");
Vector vt=onlineuser.getOnLineUser();
Enumeration e = vt.elements();
out.println("在线用户列表");
while(e.hasMoreElements()){
out.println((String)e.nextElement());
}
%>
在被直接关闭的页面中
<body οnunlοad="if(window.screenLeft>=10000&&window.screenTop>=10000){window.open('./userexit.jsp','','width=50,height=30')}">
userexit.jsp页面处理将session失效