JSP online users

Source: Internet
Author: User
/** 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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.