最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Javaweb實(shí)現(xiàn)在線人數(shù)統(tǒng)計(jì)代碼實(shí)例

 更新時(shí)間:2019年11月21日 14:21:39   投稿:yaominghui  
這篇文章主要介紹了Javaweb實(shí)現(xiàn)在線人數(shù)統(tǒng)計(jì)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了Javaweb實(shí)現(xiàn)在線人數(shù)統(tǒng)計(jì)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

session并不是瀏覽器關(guān)閉時(shí)銷毀的,而是在session失效的時(shí)候銷毀下列代碼就是監(jiān)測(cè)session創(chuàng)建、銷毀

package com.my.count;
import javax.servlet.http.*;
public class SessionCounter implements HttpSessionListener {

  private static int activeSessions = 0;
  //session創(chuàng)建時(shí)執(zhí)行
  public void sessionCreated(HttpSessionEvent se) {
    activeSessions++;
  }
  //session銷毀時(shí)執(zhí)行
  public void sessionDestroyed(HttpSessionEvent se) {
    if (activeSessions > 0)
      activeSessions--;
  }
  //獲取活動(dòng)的session個(gè)數(shù)(在線人數(shù))
  public static int getActiveSessions() {
    return activeSessions;
  }
}

接下來就是配置web.xml

<listener>
   <listener-class>
     com.my.count.SessionCounter //這里是包名加類名
   </listener-class>
 </listener>

接下來就可以在jsp頁(yè)面中使用

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.my.count.SessionCounter"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>" rel="external nofollow" >
  
  <title>My JSP 'ApplicationTest.jsp' starting page</title>
  
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">  
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" >
  -->

 </head>
 
 <body>
    在線人數(shù)為:<%=SessionCounter.getActiveSessions() %>
 </body>
</html>

不用jsp頁(yè)面 寫成接口代碼如下:

package com.wangyun.web.controllers;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class SessionCounter implements HttpSessionListener {

   static int activeSessions = 0;
  //session創(chuàng)建時(shí)執(zhí)行
  public void sessionCreated(HttpSessionEvent se) {
    activeSessions++;
  }
  //session銷毀時(shí)執(zhí)行
  public void sessionDestroyed(HttpSessionEvent se) {
    if (activeSessions > 0)
      activeSessions--;
  }
  //獲取活動(dòng)的session個(gè)數(shù)(在線人數(shù))
  public static int getActiveSessions() {
    return activeSessions;
  }

}

  /**
 	 * 在線用戶人數(shù)
 	 * @throws IOException 
 	 * @throws ServletException 
 	 */
   @RequestMapping(value="user_online", method=RequestMethod.POST, produces="text/json;charset=utf-8")
 	@ResponseBody
 	public Object user_online() throws ServletException, IOException {
 			JSONObject data = new JSONObject();
 			int number=SessionCounter.activeSessions;
 			data.put("msg",number);
 			return data.toString();
   }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

秭归县| 瓮安县| 厦门市| 乐东| 霍林郭勒市| 兴安盟| 太仆寺旗| 富阳市| 恩施市| 乐昌市| 教育| 旬阳县| 邓州市| 二连浩特市| 雅安市| 莆田市| 和硕县| 西昌市| 万年县| 克什克腾旗| 正宁县| 凯里市| 分宜县| 巴林左旗| 那曲县| 武鸣县| 平阳县| 文化| 德保县| 金秀| 湛江市| 平遥县| 溆浦县| 文安县| 遵义市| 永清县| 鄄城县| 大名县| 娄底市| 贡觉县| 奇台县|