您现在的位置: 无忧电子商务网 >> 信息学院 >> 程序开发 >> js >> 正文

彻底搞定JSP在线人数

作者:作者:未…    信息学院来源:网络收集    点击数:    更新时间:2006-8-27 我要参与讨论

  彻底搞定JSP在线人数


**这是管理user信息的类

文件名为onLineUser.java

欢迎访问javajia家(http://www.javajia.com),由yuking制作。2001.1.1
*/

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文件

文件名为onLineUser.jsp

欢迎访问javajia家(http://www.javajia.com),由yuking制作。2001.1.1
*/
%>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="onLineUser,java.util.*" %>
<jsp:useBean id="onlineuser" class="onLineUser" scope="application"/>
<html>
<head>
<title>搞定JSP在线人数</title>
</head>
<body>
<center>
<p><h1>登陆成功,欢迎您访问Java家!</h1></p>
</center>
<% session = request.getSession(false); %>
<%
String username=request.getParameter("username");
if (onlineuser.existUser(username)){
out.println("用户<font color=red>"+username+"</font>已经登陆!");
}else{
session.setMaxInactiveInterval(50); //Sesion有效时长,以秒为单位
session.setAttribute(username,onlineuser);
out.println("欢迎新用户:<font color=red>"+username+"</font>登陆到系统!");
}
ou

[1] [2] 下一页

在google里搜索更多彻底搞定JSP在线人数

Google
Web www.51ec.org
  • 上一篇信息学院:

  • 下一篇信息学院:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    我来说两句 对此文章发表了评论
      昵 称: *必填    ·注册用户·
      评 分: 1分 2分 3分 4分 5分     严禁发表危害国家安全、政治、黄色淫秽等内容的评论,用户需对自己在使用本网站服务过程中的行为承担法律责任。本站管理员有权保留或删除评论内容,评论内容只代表机友个人观点,与本网站立场无关。  
    评 论
    内 容

     
    评论列表 (最新 评论仅限网友观点!)

    推荐文章
  • 此栏目下没有推荐信息学院
  • 供求信息




    | 设为首页 | 加入收藏 | 关于我们 | 广告服务 | 联系方式 | 友情链接 | 版权申明