/** This is the class used to manage user information.
The file name is 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 () + "log on to the System" + (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 + "Exit System" + (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> handle 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); file: // 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> yuking creation </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> handle 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> webjx.com creation </p>
<P> & nbsp; </p>
<P> <a href = "logout. jsp"> exit the system </a> </p>
</Center>
</Body>
</Html>