/** This is the class used to manage user information.
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; 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 log on to the system/t" + (new date ()));
System. Out. println ("number of online users:" + getcount ());
} Else
System. Out. println (E. getname () + "already exists ");
}
Public void valueunbound (httpsessionbindingevent e ){
Users. trimtosize ();
String username = E. getname ();
Deleteuser (username );
System. Out. Print (username + "/T exit system/t" + (new date ()));
System. Out. println ("number of online users:" + getcount ());
}
}
//////////////////////////////////////// /////////////////////////////////////
<%
/** This is the JSP file for displaying online users.
The file name is 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 online users </title>
</Head>
<Body>
<Center>
<P> </Center>
<% Session = request. getsession (false); %>
<%
String username = request. getparameter ("username ");
If (onlineuser. existuser (username )){
Out. println ("User <font color = Red>" + username + "</font> already logged in! ");
} Else {
Session. setmaxinactiveinterval (50); // sesion validity period, in seconds
Session. setattribute (username, onlineuser );
Out. println ("welcome new users: <font color = Red>" + username + "</font> log on to the system! ");
}
Out. println ("<br> current number of online users: <font color = Red>" + onlineuser. getcount () + "</font> <br> ");
Vector VT = onlineuser. getonlineuser ();
Enumeration E = vt. Elements ();
Out. println ("Online User List ");
Out. println ("<Table border = 1> ");
Out. println ("<tr> <TD> User Name </TD> </tr> ");
While (E. hasmoreelements ()){
Out. println ("<tr> <TD> ");
Out. println (string) E. nextelement () + "<br> ");
Out. println ("</TD> </tr> ");
}
Out. println ("</table> ");
%>
<Center>
<P> </P>
<P> & nbsp; </P>
<%
Out. println ("<p> <a href = 'logout. jsp? Username = "+ username +" '> exit system </a> </P> ");
%>
</Center>
</Body>
</Html>
//////////////////////////////////////// //////////////////////////////////////// ////
<%
/** This is the JSP file that the user exits.
The file name is 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 online users </title>
</Head>
<Body>
<Center>
<P> </Center>
<%
String username = request. getparameter ("username ");
If (onlineuser. deleteuser (username ))
Out. println (username + "has exited the system! ");
Else
Out. println (username + "not logged on to the system! ");
%>
<Center>
<P> </P>
<P> & nbsp; </P>
<P> <a href = "logout. jsp"> exit the system </a> </P>
</Center>
</Body>
</Html>