js| Program | online | number of people online
/** This is the class that manages the user information
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 to users;
}
public void Valuebound (Httpsessionbindingevent e) {
Users.trimtosize ();
if (!existuser (E.getname ())) {
Users.add (E.getname ());
System.out.print (E.getname () + "\ t login to system \ T" + (new Date ());
System.out.println ("Number of online users:" +getcount ());
}else
System.out.println (E.getname () + "already present");
}
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 that shows the online user
File name is onlineuser.jsp
*/
%>
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "onlineuser,java.util.*"%>
<jsp:usebean id= "Onlineuser" class= "Onlineuser" scope= "Application"/>
<title> fix the number of JSP online </title>
<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> has landed!" ");
}else{
Session.setmaxinactiveinterval (50); File://Sesion effective length, in seconds
Session.setattribute (Username,onlineuser);
OUT.PRINTLN ("Welcome new users: <font color=red>" +username+ </font> login to the system!) ");
}
Out.println ("<br> Current number of users online: <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> username </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 Production </p>
<p> </p>
<%
Out.println ("<p><a href= ' logout.jsp?username=" +username+ "' > Exit System </a></p>");
%>
</center>
</body>
///////////////////////////////////////////////////////////////////
<%
/** This is the user exit JSP file
File name is logout.jsp
*/
%>
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "onlineuser,java.util.*"%>
<jsp:usebean id= "Onlineuser" class= "Onlineuser" scope= "Application"/>
<title> fix the number of JSP online </title>
<body>
<center>
<p></center>
<%
String Username=request.getparameter ("username");
if (Onlineuser.deleteuser (username))
Out.println (username+ "has exited the system!");
Else
Out.println (username+ "did not login to the system!) ");
%>
<center>
<p>yuking Production </p>
<p> </p>
<p><a href= "logout.jsp" > Exit system </a></p>
</center>
</body>