Total number of JSP online

Source: Internet
Author: User
Tags date exit
js| Online | Number of online people complete JSP online number


* * This is the class that manages user information

File name is Onlineuser.java

Welcome to Visit Javajia Home (http://www.javajia.com), made by yuking. 2001.1.1
*/

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

Welcome to Visit Javajia Home (http://www.javajia.com), made by yuking. 2001.1.1
*/
%>
<%@ 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); 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

Welcome to Visit Javajia Home (http://www.javajia.com), made by yuking. 2001.1.1
*/
%>
<%@ 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>


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.