JavaWeb学习笔记8——用Session统计在线人数

目录

一、什么是Session?

二、代码

三、截图

四、总结


一、什么是Session?

Session是另一种记录客户状态的机制,不同的是Cookie保存在客户端浏览器中,而Session是保存在服务器上的。客户端浏览器访问服务器时,服务器把客户信息以某种形式记录在服务器上,这就是Session.。当客户端浏览器再次访问时只需要从Session中查找该客户额度状态就行了。

二、代码

SessionCounter.java

package com.hedong.people;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * Application Lifecycle Listener implementation class SessionCounter
 *
 */
@WebListener
public class SessionCounter implements HttpSessionListener {
	
	private static int onlinePeople = 0;

    /**
     * Default constructor. 
     */
    public SessionCounter() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
     */
    public void sessionCreated(HttpSessionEvent se)  { 
         // TODO Auto-generated method stub
    	onlinePeople++;

    }

	/**
     * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
     */
    public void sessionDestroyed(HttpSessionEvent se)  { 
         // TODO Auto-generated method stub
    	if(onlinePeople>0) {
    		onlinePeople--;
    	}

    }
    
    public static int getOnlinePeople() {
        return onlinePeople;
    }
    
	
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SessionTest</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <listener>
      <listener-class>
          com.hedong.people.SessionCounter  
      </listener-class>
  </listener>
  
</web-app>

PeopleCount.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.hedong.people.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	在线人数为:<%=SessionCounter.getOnlinePeople()%>
</body>
</html>

三、截图

当打开不同的浏览器,人数增加。

四、总结

本例是用Session很粗糙的来实现了网站当前人数统计,并且有很多不足点,比如人数显示只能增加不能减少。后来在网上查了一下,原来是因为浏览器关闭后sessionDestroyed()不会被立即执行。解决办法是,加一个心跳检测功能,通过ajax每隔一段时间向服务器报告一个heart beating,服务端根据是否在约定的timeout时间内收到心跳信息来判断此session是否已经dead。

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大何向东流1997

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值