hyljava

          監聽器實現獲取網絡在線人數

          首先寫一個計數的類OnlineCounter

          package accp.onlinecounter;

          public class OnlineCounter {
           private static long online = 0;

           public static long getOnline() {

            return online;

           }

           public static void raise() {

            online++;

           }

           public static void reduce() {
            online--;

           }
          }


          之后寫一個實現HttpSessionEvent的類OnlineCounterListener

          package  accp.onlinecounter;

          import javax.servlet.http.HttpSessionEvent; 

            import javax.servlet.http.HttpSessionListener;
            public class OnlineCounterListener implements HttpSessionListener {
            public void sessionCreated(HttpSessionEvent hse) {
             OnlineCounter.raise();
           }

            public void sessionDestroyed(HttpSessionEvent hse) {

            OnlineCounter.reduce();
            }
            }

          在web.xml中寫listener的注冊信息
             <listener>
            <listener-class>
            accp.onlinecounter.OnlineCounterListener
            </listener-class>
              </listener>
          前臺界面寫上

            <body>
           在線人數: <%=OnlineCounter.getOnline() %><br/>
             <a href="adcourse.jsp">添加課程add course</a><br/>
             <a href="adds.jsp">添加學生add stu</a><br/>
             <a href="findallcourse.jsp">查詢課程信息 select course</a><br/>
              <a href="findallstudent.jsp">查詢學生信息 select student</a><br/>
              <a href="addstudentcourse.jsp">添加選課信息add student course</a><br/>
                <a href="querystucourse.jsp">查詢選課信息query student course</a><br/>
            
            </body>
          注意引入包即可

          posted on 2012-05-28 19:12 何云隆 閱讀(369) 評論(0)  編輯  收藏 所屬分類: Jsp

          主站蜘蛛池模板: 新蔡县| 西充县| 资中县| 六盘水市| 靖州| 错那县| 高要市| 浙江省| 个旧市| 舒城县| 平塘县| 清新县| 霸州市| 仙桃市| 措勤县| 巴塘县| 呈贡县| 淮滨县| 东安县| 绩溪县| 怀远县| 中超| 都安| 治县。| 治多县| 察哈| 宣威市| 辽中县| 房产| 岳阳县| 大名县| 永德县| 鄂尔多斯市| 溧阳市| 黄龙县| 读书| 白水县| 体育| 游戏| 耒阳市| 宁国市|