码迷,mamicode.com
首页 > 其他好文 > 详细

session的使用

时间:2019-04-14 15:59:54      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:java   ted   www   create   generate   count   highlight   ima   nta   

一、截图

技术图片

技术图片

二、代码

SessionCounter.java

package hedong.com;

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>SwpuCS</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>
          hedong.com.SessionCounter  
      </listener-class>
  </listener>
  
</web-app>

  HomePage.jsp

``````
在线人数为:<%=SessionCounter.getOnlinePeople() %>
``````

  

session的使用

标签:java   ted   www   create   generate   count   highlight   ima   nta   

原文地址:https://www.cnblogs.com/h2503652646/p/10705238.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!