JSP实例程序:统计当前在线人数

2016-02-19 13:20 6 1 收藏

有了下面这个JSP实例程序:统计当前在线人数教程,不懂JSP实例程序:统计当前在线人数的也能装懂了,赶紧get起来装逼一下吧!

【 tulaoshi.com - Web开发 】

/**这是管理user信息的类

文件名为onLineUser.java

*/
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;iusers.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;iusers.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()+" 登入到系统"+(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());
}
}

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

///////////////////////////////////////////////////////////
%
/**这是显示在线用户的jsp文件
文件名为onLineUser.jsp
*/
%
%@ 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
ph1登陆成功,欢迎您访问jzxue.com!/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); file://Sesion有效时长,以秒为单位
session.setAttribute(username,onlineuser);
out.println("欢迎新用户:font color=red"+username+"/font登陆到系统!");
}
out.println("br当前在线用户人数:font color=red"+onlineuser.getCount()+"/fontbr");
Vector vt=onlineuser.getOnLineUser();
Enumeration e = vt.elements();
out.println("在线用户列表");
out.println("table border=1");
out.println("trtd用户名/td/tr");
while(e.hasMoreElements()){
out.println("trtd");
out.println((String)e.nextElement()+"br");
out.println("/td/tr");
}
out.println("/table");
%
center
pyuking制作/p
p /p
%
out.println("pa href='Logout.jsp?username="+username+"'退出系统/a/p");
%
/center
/body
/html

///////////////////////////////////////////////////////////////////

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

%
/**这是用户退出的jsp文件
文件名为Logout.jsp
*/
%
%@ 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
ph1登陆成功,欢迎您访问jzxue.com!/h1/p
/center
%
String username=request.getParameter("username");
if(onlineuser.deleteUser(username))
out.println(username+"已经退出系统!");
else
out.println(username+"没有登陆到系统!");
%
center
pjzxue.com制作/p
p /p
pa href="Logout.jsp"退出系统/a/p
/center
/body
/html

来源:https://www.tulaoshi.com/n/20160219/1603885.html

延伸阅读
标签: ASP
  <% 'count online user without using Global.asa 'script by Narong Khotarasakit 'webmaster of ezebox.com 'report bug : info@ezebox.com sessionID = session.SessionID timeout = 5 ' set how long to keep this session in minute you can increase this number Conn_String = "DRIVER={Microsoft Access Driver (*.m...
标签: ASP
  实现在线人数统计最常用的方法就是golobal.asa结合session做,但这种方法有两个不利: 1、每个session要占用12k的服务器内存空间,为了人数+1就要付出12k内存的代价。 2、golobal.asa要放在网站的根目录下才会起作用,而且根目录只能有一个golobal,矛盾突出。 为此,有一种使用cookie和application在asp文件中实现在线统计的方法,欢迎批...
标签: Web开发
% NowUrl=request.servervariables("HTTP_REFERER") UserName = session("Wenday_sys_User") if UserName="" then    UserName="客人" end if call sqlonline() Response.Write "document.write(" & chr(34) & "  font color=#000000当前在线 "& online() &"人/font"& chr(34) & ")" 'Response.Write "document.write(" & c...
标签: ASP
  最普通的方法是用SESSION,但缺点是无法即时反应在线情况(有超时时间)和占用服务器资源(如果有100人在线就有100个SESSION,想想看8);上次(好象间隔很久了耶)介绍了一种采用APPLICATION+COOKIES的方法,但实现方法有些烦琐同时还要设一个隐藏的自刷新页面。 这种方法则避免了以上的弊病,经实践,效果不错(例子:WWW。ATERSOFT。...
标签: ASP
  count.asp: <%Server.ScriptTimeout=300 id=Trim(Request.QueryString("id")) fc=Trim(Request.QueryString("fc")) if fc="" then fc="FFFF00" bg=Trim(Request.QueryString("bg")) if bg="" then bg="000000" bc=Trim(Request.QueryString("bc")) if bc="" then bc="00FF00" sername=Request.ServerVariables("SERVER_NAME") scrn...

经验教程

715

收藏

81
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部