⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sessioncounterlistener.java

📁 JSP设计(第三版)一书源代码 JSP设计(第三版)》得到了充分的修订和更新
💻 JAVA
字号:
package com.ora.jsp.servlets;

import javax.servlet.*; 
import javax.servlet.http.*; 

/**
 * This class manages a counter for the number of active sessions in
 * an application. The counter is made available to the rest of the
 * application as a servlet context attribute of type <code>int[]</code> 
 * with one element.
 *
 * @author Hans Bergsten, Gefion software <hans@gefionsoftware.com>
 * @version 1.0
 */
public class SessionCounterListener implements HttpSessionListener { 
    private static final String COUNTER_ATTR = "session_counter";

    /**
     * Increments the counter held in the session scope.
     */
    public void sessionCreated(HttpSessionEvent hse) { 
	int[] counter = getCounter(hse); 
	counter[0]++; 
    } 
    
    /**
     * Decrements the counter held in the session scope.
     */             
    public void sessionDestroyed(HttpSessionEvent hse) { 
	int[] counter = getCounter(hse); 
	counter[0]--; 
    } 
    
    /**
     * Returns the counter held in the session scope, or a new
     * counter if it doesn't exist.
     */             
    private int[] getCounter(HttpSessionEvent hse) { 
	HttpSession session = hse.getSession(); 
	ServletContext context = session.getServletContext(); 
	int[] counter = (int[]) context.getAttribute(COUNTER_ATTR); 
	if (counter == null) { 
	    counter = new int[1]; 
	    context.setAttribute(COUNTER_ATTR, counter); 
	} 
	return counter; 
    } 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -