徹底搞定JSP線上人數

來源:互聯網
上載者:User
js|線上|線上人數 徹底搞定JSP線上人數


**這是管理user資訊的類

檔案名稱為onLineUser.java

歡迎訪問javajia家(http://www.javajia.com),由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 users;
}
public void valueBound(HttpSessionBindingEvent e) {
users.trimToSize();
if(!existUser(e.getName())){
users.add(e.getName());
System.out.print(e.getName()+"\t 登入到系統\t"+(new Date()));
System.out.println(" 線上使用者數為:"+getCount());
}else
System.out.println(e.getName()+"已經存在");
}

public void valueUnbound(HttpSessionBindingEvent e) {
users.trimToSize();
String userName=e.getName();
deleteUser(userName);
System.out.print(userName+"\t 退出系統\t"+(new Date()));
System.out.println(" 線上使用者數為:"+getCount());
}
}

/////////////////////////////////////////////////////////////////////////////
<%
/**這是顯示線上使用者的jsp檔案

檔案名稱為onLineUser.jsp

歡迎訪問javajia家(http://www.javajia.com),由yuking製作。2001.1.1
*/
%>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="onLineUser,java.util.*" %>
<jsp:useBean id="onlineuser" class="onLineUser" scope="application"/>
<html>
<head>
<title>搞定JSP線上人數</title>
</head>
<body>
<center>
<p><h1>登陸成功,歡迎您訪問Java家!</h1></p>
</center>
<% session = request.getSession(false); %>
<%
String username=request.getParameter("username");
if (onlineuser.existUser(username)){
out.println("使用者<font color=red>"+username+"</font>已經登陸!");
}else{
session.setMaxInactiveInterval(50); //Sesion有效時間長度,以秒為單位
session.setAttribute(username,onlineuser);
out.println("歡迎新使用者:<font color=red>"+username+"</font>登陸到系統!");
}
out.println("<br>當前線上使用者人數:<font color=red>"+onlineuser.getCount()+"</font><br>");
Vector vt=onlineuser.getOnLineUser();
Enumeration e = vt.elements();
out.println("線上使用者列表");
out.println("<table border=1>");
out.println("<tr><td>使用者名稱</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製作</p>
<p> </p>
<%
out.println("<p><a href=logout.jsp?username="+username+">退出系統</a></p>");
%>
</center>
</body>
</html>

////////////////////////////////////////////////////////////////////////////////////
<%
/**這是使用者退出的jsp檔案

檔案名稱為logout.jsp

歡迎訪問javajia家(http://www.javajia.com),由yuking製作。2001.1.1
*/
%>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="onLineUser,java.util.*" %>
<jsp:useBean id="onlineuser" class="onLineUser" scope="application"/>
<html>
<head>
<title>搞定JSP線上人數</title>
</head>
<body>
<center>
<p><h1>登陸成功,歡迎您訪問Java家!</h1></p>
</center>
<%
String username=request.getParameter("username");
if(onlineuser.deleteUser(username))
out.println(username+"已經退出系統!");
else
out.println(username+"沒有登陸到系統!");
%>
<center>
<p>yuking製作</p>
<p> </p>
<p><a href="logout.jsp">退出系統</a></p>
</center>
</body>
</html>


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.