分類  >  Web前端 >

Java web Session 監聽類

tags:    時間:2014-05-04 12:06:10
Java web Session 監聽類。
public class SessionListener implements HttpSessionListener { 	 	static Logger log = Logger.getLogger( 			SessionListener.class.getName());     private static Map<String, HttpSession> map = new                HashMap<String, HttpSession>();   	static private int sessionCount = 0; 	    public void sessionCreated(HttpSessionEvent event) { 	        String id = event.getSession().getId();               log.debug("session created : " + id);   	        synchronized (this) { 	            sessionCount++; 	            map.put(id, event.getSession()); 	        } 	  	       log.info("Session Created: " + event.getSession().getId()); 	       log.info("Total Sessions: " + sessionCount); 	    } 	  	    public void sessionDestroyed(HttpSessionEvent event) { 	        synchronized (this) { 	            sessionCount--; 	        } 	        log.info("Session Destroyed: " + event.getSession().getId()); 	        log.info("Total Sessions: " + sessionCount); 	    } 	    public static HttpSession getHttpSession(String sessionID)   	    {   	        return map.get(sessionID);   	    } }

in web.xml

 

 <listener> 		<listener-class>com.ipcs.listener.SessionListener</listener-class> 	  </listener>

參考頁面:http://www.coderanch.com/t/365859/Servlets/java/session-object-session-ID

推薦閱讀文章

Bookmark the permalink ,來源:互聯網