Java Code Examples for javax.servlet.http.HttpSessionEvent#getSession()

The following examples show how to use javax.servlet.http.HttpSessionEvent#getSession() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MyHttpSessionListener.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 7 votes vote down vote up
@Override
public void sessionCreated(HttpSessionEvent se) {
	HttpSession session = se.getSession();

	// 将 session 放入 map
	ApplicationConstants.SESSION_MAP.put(session.getId(), session);
	// 总访问人数++
	ApplicationConstants.TOTAL_HISTORY_COUNT++;

	// 如果当前在线人数超过历史记录,则更新最大在线人数,并记录时间
	if (ApplicationConstants.SESSION_MAP.size() > ApplicationConstants.MAX_ONLINE_COUNT) {
		ApplicationConstants.MAX_ONLINE_COUNT = ApplicationConstants.SESSION_MAP.size();
		ApplicationConstants.MAX_ONLINE_COUNT_DATE = new Date();
	}

	logger.debug("创建了一个session: {}", session);
}
 
Example 2
Source File: JsrWebSocketFilter.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Override
public void sessionDestroyed(HttpSessionEvent se) {
    HttpSessionImpl session = (HttpSessionImpl) se.getSession();
    final Session underlying;
    if (System.getSecurityManager() == null) {
        underlying = session.getSession();
    } else {
        underlying = AccessController.doPrivileged(new HttpSessionImpl.UnwrapSessionAction(session));
    }
    List<UndertowSession> connections = (List<UndertowSession>) underlying.getAttribute(SESSION_ATTRIBUTE);
    if (connections != null) {
        synchronized (underlying) {
            for (UndertowSession c : connections) {
                try {
                    c.close(new CloseReason(CloseReason.CloseCodes.VIOLATED_POLICY, ""));
                } catch (IOException e) {
                    UndertowLogger.REQUEST_IO_LOGGER.ioException(e);
                }
            }
        }
    }
}
 
Example 3
Source File: SessionListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/** HttpSessionListener interface */
   @Override
   public void sessionCreated(HttpSessionEvent sessionEvent) {
if (sessionEvent == null) {
    return;
}
HttpSession session = sessionEvent.getSession();
session.setMaxInactiveInterval(Configuration.getAsInt(ConfigurationKeys.INACTIVE_TIME));

//set server default locale for STURTS and JSTL. This value should be overwrite
//LocaleFilter class. But this part code can cope with login.jsp Locale.
if (session != null) {
    String defaults[] = LanguageUtil.getDefaultLangCountry();
    Locale preferredLocale = new Locale(defaults[0] == null ? "" : defaults[0],
	    defaults[1] == null ? "" : defaults[1]);
    session.setAttribute(LocaleFilter.PREFERRED_LOCALE_KEY, preferredLocale);
    Config.set(session, Config.FMT_LOCALE, preferredLocale);
}
   }
 
Example 4
Source File: SessionDestroyedListener.java    From boubei-tss with Apache License 2.0 6 votes vote down vote up
public void sessionCreated(HttpSessionEvent event) {
    HttpSession session = event.getSession();
    
    // 设置 session 的过期时间
    if(session.isNew()){
        String configValue = ParamConfig.getAttribute(PX.SESSION_CYCLELIFE_CONFIG);
        try {
        	int cycleLife = Integer.parseInt(configValue);
session.setMaxInactiveInterval(cycleLife); // 以秒为单位
        } 
        catch(Exception e) { }
    }
    
    String sessionId = session.getId();
    String appCode = Context.getApplicationContext().getCurrentAppCode();
    log.debug("应用【" + appCode + "】里 sessionId为:" + sessionId
            + " 的session创建完成,有效期为:" + session.getMaxInactiveInterval() + " 秒 ");
    
    Context.sessionMap.put(sessionId, session);
}
 
Example 5
Source File: OnlineSessionStore.java    From rebuild with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void sessionDestroyed(HttpSessionEvent event) {
	if (LOG.isDebugEnabled()) {
		LOG.info("Destroyed session - " + event);
	}
	
	HttpSession s = event.getSession();
	if (ONLINE_SESSIONS.contains(s)) {
		ONLINE_SESSIONS.remove(s);
	} else {
		for (Map.Entry<ID, HttpSession> e : ONLINE_USERS.entrySet()) {
			if (s.equals(e.getValue())) {
				ONLINE_USERS.remove(e.getKey());
				break;
			}
		}
	}
	
	// Logout time
	ID loginId = (ID) s.getAttribute(LoginControll.SK_LOGINID);
	if (loginId != null) {
		Record logout = EntityHelper.forUpdate(loginId, UserService.SYSTEM_USER);
		logout.setDate("logoutTime", CalendarUtils.now());
		Application.getCommonService().update(logout);
	}
}
 
Example 6
Source File: MySessionListener.java    From mytwitter with Apache License 2.0 5 votes vote down vote up
@Override
public void sessionDestroyed(HttpSessionEvent event) {
	HttpSession session = event.getSession();
	Users user = (Users) session.getAttribute("user");
	if (user == null) {
		return;
	}
	ServletContext servletContext = event.getSession().getServletContext();
	servletContext.removeAttribute(((Users) session.getAttribute("user")).getUname());
	Integer onlineNum = (Integer) servletContext.getAttribute("onlineNum");
	if (onlineNum > 0) {
		servletContext.setAttribute("onlineNum", onlineNum - 1);
	}
	Object signinid = session.getAttribute("signinid");

	if (user == null || signinid == null) {
		return;
	}
	int uid = user.getUid();
	Timestamp sdtime = Times.getSystemTime();
	usersDao.updateOnline(0, uid);
	signinDao.updateSignin((Integer) signinid, sdtime);

	Object adid = session.getAttribute("adid");
	Admins admin = (Admins) session.getAttribute("admin");
	if (admin == null || adid == null) {
		return;
	}

	int aid = admin.getAid();
	adminsDao.updateOnline(0, aid);
	adloginDao.updateSignin((Integer) adid, sdtime);
}
 
Example 7
Source File: MyHttpSessionListener.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Override
public void sessionDestroyed(HttpSessionEvent se) {
	HttpSession session = se.getSession();
	// 将session从map中移除
	ApplicationConstants.SESSION_MAP.remove(session.getId());

	logger.debug("销毁了一个session: {}", session);
}
 
Example 8
Source File: SessionListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
   @Override
   public void sessionDestroyed(HttpSessionEvent sessionEvent) {
if (SessionListener.authenticationManager == null) {
    try {
	InitialContext initialContext = new InitialContext();
	SessionListener.authenticationManager = (CacheableManager<?, Principal>) initialContext
		.lookup("java:jboss/jaas/lams/authenticationMgr");
    } catch (NamingException e) {
	SessionListener.log.error("Error while getting authentication manager.", e);
    }
}

// clear the authentication cache when the session is invalidated
HttpSession session = sessionEvent.getSession();
if (session != null) {
    SessionManager.removeSessionByID(session.getId(), false, true);

    UserDTO userDTO = (UserDTO) session.getAttribute(AttributeNames.USER);
    if (userDTO != null) {
	String login = userDTO.getLogin();
	Principal principal = new SimplePrincipal(login);
	SessionListener.authenticationManager.flushCache(principal);

	// remove obsolete mappings to session
	// the session is either already invalidated or will be very soon by another module
	SessionManager.removeSessionByLogin(login, false);
    }
}
   }
 
Example 9
Source File: CsrfGuardHttpSessionListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void sessionCreated(HttpSessionEvent event) {
    HttpSession session = event.getSession();
    CsrfGuard csrfGuard = CsrfGuard.getInstance();
    csrfGuard.updateToken(session);
    // Check if should generate tokens for protected resources on current session
    if (csrfGuard.isTokenPerPageEnabled() && csrfGuard.isTokenPerPagePrecreate()
            && !SessionUtils.tokensGenerated(session)) {
        csrfGuard.generatePageTokensForSession(session);
    }

}
 
Example 10
Source File: HttpSessionIdListenerTest.java    From piranha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Handle the session id changed event.
 *
 * @param event the event.
 * @param oldSessionId the old session id.
 */
@Override
public void sessionIdChanged(HttpSessionEvent event, String oldSessionId) {
    HttpSession session = event.getSession();
    session.getServletContext().setAttribute("newSessionId", session.getId());
    session.getServletContext().setAttribute("oldSessionId", oldSessionId);
}
 
Example 11
Source File: UserCounterListener.java    From jivejdon with Apache License 2.0 5 votes vote down vote up
public void sessionDestroyed(HttpSessionEvent se) {
	activeSessions--;
	HttpSession session = se.getSession();
	String username = (String) session.getAttribute("online");
	if (username == null) {
		return;
	}
	List userList = (List) servletContext.getAttribute(OnLineUser_KEY);
	userList.remove(username);

}
 
Example 12
Source File: SessionListener.java    From PedestrianDetectionSystem with Apache License 2.0 5 votes vote down vote up
@Override
public void sessionDestroyed(HttpSessionEvent se) {
    HttpSession session = se.getSession();
    ServletContext context = session.getServletContext();
    Integer subFlag = (Integer)session.getAttribute("sub_flag");
    if(subFlag == null){
        context.setAttribute("onlineCount", (Integer)context.getAttribute("onlineCount") - 1);
    }
}
 
Example 13
Source File: SessionListener.java    From PedestrianDetectionSystem with Apache License 2.0 5 votes vote down vote up
@Override
public void sessionCreated(HttpSessionEvent se) {
    HttpSession session = se.getSession();
    ServletContext context = session.getServletContext();
    if (context.getAttribute("onlineCount") == null){
        context.setAttribute("onlineCount", 1);
    }else {
        context.setAttribute("onlineCount", (Integer)context.getAttribute("onlineCount") + 1);
    }

}
 
Example 14
Source File: SesssionEventListener.java    From oslits with GNU General Public License v3.0 5 votes vote down vote up
public void sessionDestroyed(HttpSessionEvent se) {
	HttpSession session = se.getSession();
	synchronized( sessionMonitor ) {
		sessionMonitor.remove(session.getId());
		System.out.println(session.getId() + " : 세션 제거");
	}
}
 
Example 15
Source File: SesssionEventListener.java    From oslits with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void sessionCreated(HttpSessionEvent se) {
	HttpSession session = se.getSession();
	synchronized(sessionMonitor) {
		sessionMonitor.put(session.getId(), session);
		System.out.println(session.getId() + " : 세션 저장");
	}
}
 
Example 16
Source File: CustomListener.java    From bidder with Apache License 2.0 5 votes vote down vote up
@Override
public void sessionDestroyed(HttpSessionEvent ev) {
	HttpSession session = ev.getSession();
	String id = session.getId();

	sessions.remove(session);
	//System.out.println("SESSION: " + id + " was destroyed");
	
}
 
Example 17
Source File: CustomListener.java    From bidder with Apache License 2.0 5 votes vote down vote up
@Override
public void sessionCreated(HttpSessionEvent ev) {
	HttpSession session = ev.getSession();
	String id = session.getId();

	sessions.add(session);
	//System.out.println("SESSION: " + id + " was created");
	
}
 
Example 18
Source File: HttpSessionCleanUpListener.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
	HttpSession session = httpSessionEvent.getSession();
	String sessionID = session.getId();
	ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());
	
	// Cleanup FutureHolder
	FutureHolderMap futureHolder = (FutureHolderMap) applicationContext.getBean("futureHolder");
	List<String> keysToRemove = new ArrayList<>();
	for (String key : futureHolder.keySet()) {
		if (key.endsWith("@" + sessionID) ) {
			keysToRemove.add(key);
		}
	}
	for (String removeMe : keysToRemove) {
		futureHolder.remove(removeMe);
	}
	
	// Remove all download data and associated files
	DownloadService downloadService = (DownloadService) applicationContext.getBean("DownloadService");
	downloadService.removeAllDownloadData(session);
	
	// Cleanup grid recycle bin
	ComAdmin admin = (ComAdmin) session.getAttribute(AgnUtils.SESSION_CONTEXT_KEYNAME_ADMIN);
       if (admin != null) {
           ComGridTemplateService gridTemplateService = applicationContext.getBean("GridTemplateService", ComGridTemplateService.class);
           gridTemplateService.deleteRecycledChildren(admin.getCompanyID());
       }
       
       // Cleanup waiting interactive imports
       ProfileImportWorker profileImportWorker = (ProfileImportWorker) session.getAttribute(ProfileImportAction.PROFILEIMPORTWORKER_SESSIONKEY);
	if (profileImportWorker != null && profileImportWorker.isWaitingForInteraction()) {
		profileImportWorker.cleanUp();
		session.removeAttribute(ProfileImportAction.PROFILEIMPORTWORKER_SESSIONKEY);
		logger.info("Canceled interactively waiting ProfileImport for session: " + sessionID + " " + (admin != null ? "admin: " + admin.getUsername() : ""));
	}
}
 
Example 19
Source File: SessionListener.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
@Override
public void sessionCreated(HttpSessionEvent arg0) {

	HttpSession session =  arg0.getSession();

   	logger.info("Session created {}", session.getId());

   	User user = (User)session.getAttribute(BeanUtil.KEY_USER);

   	if(user != null) {

   		logger.info("Session contains user instance: {}", user);

   	} else {

   		session.setAttribute(BeanUtil.KEY_USER, getDefaultUser());

   	}

}
 
Example 20
Source File: SessionListener.java    From sailfish-core with Apache License 2.0 3 votes vote down vote up
@Override
public void sessionDestroyed(HttpSessionEvent arg0) {

	HttpSession session = arg0.getSession();

   	Object user = session.getAttribute(BeanUtil.KEY_USER);

   	logger.info("Session {} of {} destroyed", session.getId(), user);

}