日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

用JSP的Session實現(xiàn)在線用戶統(tǒng)計_JSP教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:weblogic的jsp問題解決方法
在做項目的時候,jsp在運行的時候出現(xiàn)了一些問題,現(xiàn)將我的問題解決方法做一個小結(jié),供以后作項目的參考。 模板無憂 問題1: weblogic 的數(shù)據(jù)庫連接數(shù)目在程序運行中不斷增長,最后

現(xiàn)在對于處理在線用戶,有幾種不同的處理方法。

  一種是頁面刷新由用戶控制,服務(wù)器端控制一個超時時間比如30分鐘,到了時間之后用戶沒有動作就被踢出。這種方法的優(yōu)點是,如果用戶忘了退出,可以防止別人惡意操作。缺點是,如果你在做一件很耗時間的事情,超過了這個時間限制,submit的時候可能要再次面臨登陸。如果原來的葉面又是強制失效的話,就有可能丟失你做的工作。在實現(xiàn)的角度來看,這是最簡單的,Server端默認實現(xiàn)的就是這樣的模式。

  另一種方式是,站點采用框架結(jié)構(gòu),有一個Frame或者隱藏的iframe在不斷刷新,這樣你永遠不會被踢出,但是服務(wù)器端為了判斷你是否在線,需要定一個發(fā)呆時間,如果超過這個發(fā)呆時間你除了這個自動刷新的頁面外沒有刷新其他頁面的話,就認為你已經(jīng)不在線了。采取這種方式的典型是xici.net。 他的優(yōu)點是可以可以利用不斷的刷新實現(xiàn)一些類似server-push的功能,比如網(wǎng)友之間發(fā)送消息。

  不管哪一種模式,為了實現(xiàn)瀏覽當前所有的在線用戶,還需要做一些額外的工作。servlet API中沒有得到Session列表的API。

  可以利用的是Listener. Servlet 2.2和2.3規(guī)范在這里略微有一些不一樣。2.2中HttpSessionBindingListener可以實現(xiàn)當一個HTTPSession中的Attribute變化的時候通知你的類。而2.3中還引入了HttpSessionAttributeListener.鑒于我使用的環(huán)境是Visual age for java 4和JRun server 3.1,他們還不直接支持Servlet 2.3的編程,這里我用的是HttpSessionBindingListener.

  需要做的事情包括做一個新的類來實現(xiàn)HttpSessionBindingListener接口。這個接口有兩個方法:

public void valueBound(HttpSessionBindingEvent event)
public void valueUnbound(HttpSessionBindingEvent event)

  當你執(zhí)行Session.addAttribute(String,Object)的時候,如果你已經(jīng)把一個實現(xiàn)了HttpSessionBindingListener接口的類加入為Attribute,Session會通知你的類,調(diào)用你的valueBound方法。相反,Session.removeAttribute方法對應(yīng)的是valueUndound方法。

public class HttpSessionBinding implements javax.servlet.http.HttpSessionBindingListener
{
 ServletContext application = null;

 public HttpSessionBinding(ServletContext application)
 {
  super();
  if (application ==null)
   throw new IllegalArgumentException("Null application is not accept.");
  this.application = application;
 }

 public void valueBound(javax.servlet.http.HttpSessionBindingEvent e)
 {
  Vector activeSessions = (Vector) application.getAttribute("activeSessions");
  if (activeSessions == null)
  {
   activeSessions = new Vector();
  }

  JDBCUser sessionUser = (JDBCUser)e.getSession().getAttribute("user");
  if (sessionUser != null)
  {
   activeSessions.add(e.getSession());
  }
  application.setAttribute("activeSessions",activeSessions);
 }

 public void valueUnbound(javax.servlet.http.HttpSessionBindingEvent e)
 {
  JDBCUser sessionUser = (JDBCUser)e.getSession().getAttribute("user");
  if (sessionUser == null)
  {
   Vector activeSessions = (Vector) application.getAttribute("activeSessions");
   if (activeSessions != null)
   {
    activeSessions.remove(e.getSession().getId());
    application.setAttribute("activeSessions",activeSessions);
   }
  }
 }
}

  假設(shè)其中的JDBCUser類是一個任意User類。在執(zhí)行用戶登錄時,把User類和HttpSessionBinding類都加入到Session中去。

  這樣,每次用戶登錄后,在application中的attribute "activeSessions"這個vector中都會增加一條記錄。每當session超時,valueUnbound被觸發(fā),在這個vector中刪去將要被超時的session.

public void login()
throws ACLException,SQLException,IOException
{
 /* get JDBC User Class */
 if (user != null)
 {
  logout();
 }
 {
  // if session time out, or user didn't login, save the target url temporary.

  JDBCUserFactory uf = new JDBCUserFactory();

  if ( (this.request.getParameter("userID")==null) || (this.request.getParameter("password")==null) )
  {
   throw new ACLException("Please input a valid userName and password.");
  }

  JDBCUser user = (JDBCUser) uf.UserLogin(
   this.request.getParameter("userID"),
   this.request.getParameter("password") );
   user.touchLoginTime();
   this.session.setAttribute("user",user);
   this.session.setAttribute("BindingNotify",new HttpSessionBinding(application));
  }
 }

  Login的時候,把User和這個BindingNotofy目的的類都加入到session中去。logout的時候,就要主動在activeSessions這個vector中刪去這個session.

public void logout()
throws SQLException,ACLException
{
 if (this.user == null && this.session.getAttribute("user")==null)
 {
  return;
 }

 Vector activeSessions = (Vector) this.application.getAttribute("activeSessions");
 if (activeSessions != null)
 {
  activeSessions.remove(this.session);
  application.setAttribute("activeSessions",activeSessions);
 }

 java.util.Enumeration e = this.session.getAttributeNames();

 while (e.hasMoreElements())
 {
  String s = (String)e.nextElement();
  this.session.removeAttribute(s);
 }
 this.user.touchLogoutTime();
 this.user = null;
}

   這兩個函數(shù)位于一個HttpSessionManager類中.這個類引用了jsp里面的application全局對象。這個類的其他代碼和本文無關(guān)且相當長,我就不貼出來了。

分享:JSP XML實現(xiàn)網(wǎng)頁內(nèi)容動態(tài)顯示的方案
一、xml技術(shù)簡介   XML(eXtensible Markup Language??可擴展標記語言)是一種 擴展的源標記語言,是可以定義其他語言的語言。   它是SGML的一個簡化子集,這個子集是專為Web

來源:模板無憂//所屬分類:JSP教程/更新時間:2008-08-22
相關(guān)JSP教程