欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
JSP Session 在線(xiàn)人數統計

1、HttpSessionBindingListener接口介紹
      如果一個(gè)對象實(shí)現了HttpSessionBindingListener接口,當這個(gè)對象被綁定到Session中或者從session中被刪除時(shí),Servlet容器會(huì )通知這個(gè)對象,而這個(gè)對象在接收到通知后,可以做一些初始化或清除狀態(tài)的操作。
     javax.servlet.http.HttpSessionBindingListener接口提供了以下方法:
     public void valueBound(HttpSessionBindingEvent event)
     當對象正在被綁定到Session中,Servlet容器調用這個(gè)方法來(lái)通知該對象。
     public void valueUnbound(HttpSessionBindingEvent event)
     當從Session中刪除對象時(shí),Servlet容器調用這個(gè)方法來(lái)實(shí)現了HttpSessionBindingListener接口的對象,而這個(gè)對象可以利用HttpSessionBindingEvent對象來(lái)訪(fǎng)問(wèn)與它相聯(lián)系的HttpSession對象。     Javax.Servlet.http.HttpSessionBindingEvent類(lèi)提供了以下兩種方法。
      public HttpSessionBindingEvent(HttpSession session,java.lang.String name)
      public HttpSessionBindingEvent(HttpSession session,java.lang.string name,java.lang.Object value)
     上面兩個(gè)構造一個(gè)事件對象,當一個(gè)對象被綁定到Session中或者從Session中被刪除時(shí),用這個(gè)事件對象來(lái)通知它。
     public java.lang.String getName()
     返回綁定到Session中或者從session中刪除的屬性的名字。
     public java.lang.Object getValue()
     返回添加、刪除或替換的屬性的值。如果屬性被添加或者被刪除,這個(gè)方法返回屬性的值。如果屬性被替換,這個(gè)方法返回屬性先前的值。
     public HttpSession getSession()
     返回HttpSession對象。
2、在線(xiàn)人數統計程序:
      利用HttpSessionBindingListener接口,編寫(xiě)一個(gè)在線(xiàn)人數統計的等程序。當一個(gè)用戶(hù)登陸時(shí),添加Session到在線(xiàn)人名單中,當一個(gè)用戶(hù)退出時(shí)或者Session超時(shí)時(shí),從在線(xiàn)人名單中刪除該用戶(hù)。
在UserList這個(gè)類(lèi)中,應用單件模式,向程序提供一個(gè)全局訪(fǎng)問(wèn)點(diǎn)。

import java.util.Vector;
import java.util.Enumeration;

public class UserList
{
    private static final UserList userList = new UserList();
    private Vector v = new Vector();
   
    private UserList()
    {
        //v = new Vector();
    }
    public static UserList getInstance()
    {
        return userList;
    }
    //將用戶(hù)登陸ID保存到Vector中
    public void addUser(Object dlid) throws Exception
    {
        try{
           if ( dlid != null)
           {
               if (  v.indexOf(dlid) >= 0)//判斷是否已經(jīng)存在
                   return ;                  
               //可能的操作
               Yhjbxx yh = new Yhjbxx();
               yh.SetYhjbxxDqzt(Integer.parseInt(dlid.toString()),"1");//改寫(xiě)數據庫供其它應用讀取。
               //添加登錄ID
               v.addElement(dlid);
           }
        }
         catch(Exception ex)
        {
             Log.writeDebug(ex.toString());           
        }
        finally{
        }
    }
    /**
     * 判斷是否存在會(huì )話(huà)
     */
    public boolean IsExist(Object dlid)throws Exception
    {
        try{
             if (  v.indexOf(dlid) >= 0)
                   return true;              
            return false;
        }
        catch(Exception ex)
        {
            Log.writeDebug(ex.toString());
            return false;
        }
    }
   
    //刪除用戶(hù)登錄ID
    public void RemoveUser(Object dlid)throws Exception
    {
        try{
           if ( dlid != null )
           {  
              //修改數據庫
               Yhjbxx yh = new Yhjbxx();
               yh.SetYhjbxxDqzt(Integer.parseInt(dlid.toString()),"");
              //移除用戶(hù)登錄ID
               v.removeElement(dlid);
           }
        }
        catch(Exception ex)
        {    
            Log.writeDebug(ex.toString()); //寫(xiě)日志
        }
        finally{
        }
    }
    //返回Vector枚舉
    public Enumeration getUserList()
    {
        return v.elements();
    }
    //返回在線(xiàn)人數
    public int getUserCount()
    {
        return v.size();
    }
}

User 類(lèi)實(shí)現了HttpSessionBindingListener接口,表示登錄用戶(hù)
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class User implements
HttpSessionBindingListener

    //用戶(hù)登錄ID
    private int dlid;
    private UserList u1 = UserList.getInstance();    
    public User(int dlid)
    {
        this.dlid = dlid;
    }
    public User()
    {
    }
    public void setdlid(int v)
    {
        this.dlid = v;
    }
    public int getdlid()
    {
        return this.dlid;
    }
    //判斷用戶(hù)是否存在
    public boolean IsExist(int dlid)throws Exception
    {
        try
        {
            Object o = Integer.toString(dlid);
          return u1.IsExist(o);
        }
        catch(Exception ex)
        {
            Log.writeDebug(ex.toString());
            return false;
        }
    }
  
  
  public void valueBound(HttpSessionBindingEvent event)
    { 
        try{
           Object o = Integer.toString(dlid);//(Object)dlid;
           u1.addUser(o);
       }
       catch(Exception ex)
       {
          Log.writeDebug(ex.toString());         
       }
    }
    public void valueUnbound(HttpSessionBindingEvent event)
    {
        try{
           Object o = Integer.toString(dlid);
           u1.RemoveUser(o);
        }
        catch(Exception ex)
        {
             Log.writeDebug(ex.toString());
        }
    }
}

登錄時(shí)添加會(huì )話(huà):
User user = new User(y.getid());
session.setAttribute("user",user);  

退出時(shí)刪除會(huì )話(huà):
User us = (User)session1.getAttribute("user");
  if ( us != null )
  {
   if ( us.IsExist(us.getdlid()))
       session1.invalidate();
  }

退出時(shí)刪除會(huì )話(huà)并關(guān)閉瀏覽器Servelt

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

public class LogoutServlet extends HttpServlet
{
    public void doGet(HttpServletRequest req,HttpServletResponse resp)
                throws ServletException,IOException
    {
        resp.setContentType("text/html;charset=gb2312");
       
        HttpSession session = req.getSession();
        User user = (User)session.getAttribute("user");
        session.invalidate();
        PrintWriter out = resp.getWriter();
        StringBuffer strbuffer = new StringBuffer();
        strbuffer.append("<body>");
        strbuffer.append("<script loaguage=\"javascript\">");
        strbuffer.append("var ua=navigator.userAgent;");
        strbuffer.append("var ie=navigator.appName==\"Microsoft Internet Explorer\"?true:false;");
        strbuffer.append("if(ie){");
        strbuffer.append("var Ieversion=parseFloat(ua.substring(ua.indexOf(\"MSIE\")+5,ua.indexOf(\";\",ua.indexOf(\"MSIE \"))));");
        strbuffer.append("if(Ieversion< 5.5){");
        strbuffer.append(" var str  = '<object id=noTipClose classid=\"clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11\">';");
        strbuffer.append("str += '<param name=\"Command\" value=\"Close\"></object>';");
        strbuffer.append("document.body.insertAdjacentHTML(\"beforeEnd\", str);");
        strbuffer.append("document.all.noTipClose.Click();");
        strbuffer.append(" }");
        strbuffer.append(" else{");       
        strbuffer.append("window.opener = null;");
        strbuffer.append("window.close();");
        strbuffer.append("}");
        strbuffer.append("}");
        strbuffer.append("else {");
        strbuffer.append("window.close();");
        strbuffer.append("}");       
        strbuffer.append("</script>");
        strbuffer.append("</body>");
        out.print( strbuffer.toString());
    }
}

參考:JAVA WEB 開(kāi)發(fā)詳解_XML+XSLT+Servlet+JSP深入開(kāi)發(fā)剖析與實(shí)例應用

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
java 頁(yè)面在線(xiàn)訪(fǎng)問(wèn)人數統計和在線(xiàn)登錄人數統計
GPS位置更新頻繁,返回的Location異常,數據不準確
Java 統計用戶(hù)是否在線(xiàn)狀態(tài) | 程序員的資料庫
Servlet 中的 Listener 的應用
【 servlet的session管理】
在Struts2下使用[攔截器]配合[session監聽(tīng)器]實(shí)現在線(xiàn)會(huì )員統計與防止重復登錄
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久