中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Jsp使用HttpSessionBindingListener實(shí)現(xiàn)在線人數(shù)記錄

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

onLineUser.java 繼承HttpSessionBindingListener實(shí)現(xiàn)在線人數(shù)記錄功能

package com.trs;

import java.util.*;  
import javax.servlet.http.*;  
import javax.servlet.*;  

/**
*HttpSessionBindingListener接口有兩方需要實(shí)現(xiàn)的方法:
*public synchronized void valueBound(HttpSessionBindingEvent httpsessionbindingevent)
*public synchronized void valueUnbound(HttpSessionBindingEvent httpsessionbindingevent)
*Session創(chuàng)建的時(shí)候Servlet容器將會(huì)調(diào)用valueBound方法;Session刪除的時(shí)候則調(diào)用valueUnbound方法.
*/
public class onLineUser implements HttpSessionBindingListener
{  
    public onLineUser()
    { 
    } 

    //保存在線用戶的向量
    private Vector users=new Vector();

    //得到用戶總數(shù)
    public int getCount()
    { 
        users.trimToSize(); 
        return users.capacity(); 
    }

    //判斷是否存在指定的用戶
    public boolean existUser(String userName)
    { 
        users.trimToSize(); 
        boolean existUser=false; 
        for (int i=0;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
            { 
                if(userName.equals((String)users.get(i)))
                { 
                    currUserIndex=i; 
                    break; 
                } 
            } 
            if (currUserIndex!=-1)
            { 
                users.remove(currUserIndex); 
                users.trimToSize(); 
                return true; 
            } 
        } 
        return false; 
    }

    //得到當(dāng)前在線用戶的列表
    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ǒng)\t"+(new Date())); 
            System.out.println("     在線用戶數(shù)為:"+getCount()); 
        }else 
            System.out.println(e.getName()+"已經(jīng)存在"); 
    }  

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

logout.jsp

<%@ page contentType="text/html;charset=GB2312" pageEncoding="GBK"%>
<%@ page import="com.trs.onLineUser,java.util.*" %>  
<jsp:useBean id="onlineuser" class="com.trs.onLineUser" scope="application"/> 
<html>
<head>
<title>show</title>
</head>  
<body>
<%  
String name=(String)session.getValue("name");
if(name!=null && name.length()!=0)
{
if(onlineuser.deleteUser(name)) 
  out.println(name+"已經(jīng)退出系統(tǒng)!"); 
else 
  out.println(name+"沒有登陸到系統(tǒng)!"); 
}
%>
</body>
</html>

online.jsp

<%@ page contentType="text/html;charset=GB2312" pageEncoding="GBK"%>
<%@page import="com.trs.onLineUser,java.util.*" %>
<html>
</body>
<%
String name=request.getParameter("name");
String password=request.getParameter("password");

if(name!=null && password!=null)
{
Cookie cookie1=new Cookie("name", name); 
cookie1.setMaxAge(100000);
response.addCookie(cookie1);

Cookie cookie2=new Cookie("password", password); 
cookie2.setMaxAge(100000);
response.addCookie(cookie2);
out.println("完成書寫Cookie!");
}
else
{
out.println("書寫失敗!");
}
%>
</body>
</html>

需要說明的是這種方式適合只有單臺(tái)服務(wù)器的小網(wǎng)站使用,如果網(wǎng)站有多臺(tái)web server則不能使用這種方式記錄在線人數(shù)。

標(biāo)簽: 服務(wù)器

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:Java解析XML文件

下一篇:java使用SAX解析xml