熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

HttpSessionListener實現統計在線人數

2013-11-23 18:57:18  來源: Java核心技術 

  HttpSessionListener是個session監聽器它有兩個方法publicvoidsessionCreated(HttpSessionEventevent){}和publicvoidsessionDestroyed(HttpSessionEventevent){}前者是在session被創建的時候執行後者是在session被銷毀的時候執行通過對當前session的監聽達到統計在線人數的效果
  代碼如下
  首先建一個監聽類CountLineListener實現HttpSessionListener接口並添加未實現的方法sessionCreated(){}和sessionDestroyed(){}:
  [java]
  packagecomtestlistener;
  importjavaxservletServletContext;
  importjavaxservlethttpHttpSessionEvent;
  importjavaxservlethttpHttpSessionListener;
  publicclassCountLineListenerimplementsHttpSessionListener{
  /***********
  *創建session時調用
  */
  publicvoidsessionCreated(HttpSessionEventevent){
  Systemoutprintln(創建session……
  ServletContextcontext=eventgetSession()getServletContext()
  Integercount=(Integer)contextgetAttribute(count
  if(count==null){
  count=newInteger(
  }else{
  intco=countintValue()
  count=newInteger(co+
  }
  Systemoutprintln(當前用戶人數+count)
  contextsetAttribute(countcount)//保存人數
  }
  /************
  *銷毀session時調用
  */
  publicvoidsessionDestroyed(HttpSessionEventevent){
  Systemoutprintln(銷毀session……
  ServletContextcontext=eventgetSession()getServletContext()
  Integercount=(Integer)contextgetAttribute(count
  intco=countintValue()
  count=newInteger(co
  contextsetAttribute(countcount)
  Systemoutprintln(當前用戶人數+count)
  }
  }
  監聽類寫好了接下來就要在webxml裡配置此監聽類添加代碼
  [java]
  <listener>
  <listenerclass>comtestlistenerCountLineListener</listenerclass>
  </listener>
  針對以上可以結合servlet寫個小例子
  ()loginjsp
  [html]
  <%@pagelanguage=javaimport=javautil*pageEncoding=UTF%>
  <!DOCTYPEHTMLPUBLIC//WC//DTDHTMLTransitional//EN>
  <html>
  <head>
  <title>MyJSPindexjspstartingpage</title>
  <metahttpequiv=pragmacontent=nocache>
  <metahttpequiv=cachecontrolcontent=nocache>
  <metahttpequiv=expirescontent=>
  <metahttpequiv=keywordscontent=keywordkeywordkeyword>
  <metahttpequiv=descriptioncontent=Thisismypage>
  </head>
  <body>
  <formmethod=POSTaction=<%=requestgetContextPath()%>/MyServlet>
  <inputtype=textname=username/>
  <br/><inputtype=submitvalue=登錄/>
  </form>
  </body>
  </html>

  點擊登錄>MyServlet
  ()MyServletjava
  [java]
  packagecomtestservlet;
  importjavaioIOException;
  importjavaxservletServletException;
  importjavaxservlethttpHttpServlet;
  importjavaxservlethttpHttpServletRequest;
  importjavaxservlethttpHttpServletResponse;
  publicclassMyServletextendsHttpServlet{
  publicvoiddoGet(HttpServletRequestrequestHttpServletResponseresponse)
  throwsServletExceptionIOException{
  thisdoPost(requestresponse)
  }
  publicvoiddoPost(HttpServletRequestrequestHttpServletResponseresponse)
  throwsServletExceptionIOException{
  Stringuser=requestgetParameter(username
  requestgetSession()setAttribute(useruser)
  requestgetRequestDispatcher(/indexjspforward(requestresponse)
  }
  }
  登錄就跳到首頁indexjsp顯示在線人數
  [html]
  <%@pagelanguage=javaimport=javautil*pageEncoding=UTF%>
  <!DOCTYPEHTMLPUBLIC//WC//DTDHTMLTransitional//EN>
  <html>
  <head>
  <title>MyJSPindexjspstartingpage</title>
  <metahttpequiv=pragmacontent=nocache>
  <metahttpequiv=cachecontrolcontent=nocache>
  <metahttpequiv=expirescontent=>
  <metahttpequiv=keywordscontent=keywordkeywordkeyword>
  <metahttpequiv=descriptioncontent=Thisismypage>
  </head>
  <body>
  這裡是首頁當前訪問量
  <%
  ServletContextcontext=sessiongetServletContext()
  Integercount=(Integer)contextgetAttribute(count
  %>
  <%=count%>
  <br/>
  當前用戶${sessionScopeuser}
  </body>
  </html>
  這樣就簡單實現統計當前在線人數的效果了如果在頁面有一個退出系統的鏈接可以調用sessioninvalidate()執行清除session這樣在線人數就會那如果用戶沒有點擊退出系統而是直接關閉浏覽器呢?我自己做了個測試在關閉浏覽器一會兒的話服務器端會自動執行sessionDestroyed()方法進行銷毀session此時用戶人數但是有時候又不會執行不知道為什麼自己還是慢慢研究吧……


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26031.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.