2016-11-24 63 views
0

我需要获取所有活动会话的列表,以便我可以管理它们。基本上我需要管理应用程序中的所有登录用户。 使用HttpServletRequest的REQ我能够获得当前会话,但 需要得到所有会话在RESTEasy实现中获取所有活动的HttpSession

事情是这样的:

 public EmployeeTO getEmployeeById(int id, HttpServletRequest req) { 
     EmployeeTO employeeTO = null; 
     try{ 

      HttpSession session = req.getSession(); 
      HttpSessionContext httpSessionContext = session.getSessionContext(); 


     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     return employeeTO; 
    } 

我使用REST风格的实施与JASS用于登录

回答

1

我有一个显示所有活动用户列表的屏幕。如果我检查一个 用户并单击关闭会话。我需要终止该用户会话。 要做到这一点,我需要有可访问的会话。

使用HttpServletRequest,您将只能得到当前请求的(用户)session对象。但是,如果你想跟踪所有session对象,你可以做,通过实施HttpSessionListener,如下图所示:

public class MyProjectSessionListenerAndInvalidator 
           implements HttpSessionListener { 

    private static Map<String,Session> sessions = new HashMap<>(); 

     @Override 
     public void sessionCreated(HttpSessionEvent event) { 
      //add your code here, 
      //this will be invoked whenever there is a new session object created 
      //Get the newly created session 
      Session session = event.getSession(); 
      //get userId or unique field from session 
      sessions.put(userId, session); 
     } 

     @Override 
     public void sessionDestroyed(HttpSessionEvent event) { 
     //add your code here 
     //this will be invoked whenever there is a new session object removed 

      //Get the removed session 
      Session session = event.getSession(); 
      //get userId or unique field from session 
      sessions.remove(userId); 
     } 

     public R getSessions() { 
     //add code here 
     } 

     public void invalidateSession(String userId) { 
     //add code here 
     } 
} 

N.B:我建议使用getSessions()invalidateSession()仔细

+0

事情是我有一个屏幕,显示所有活动用户列表。如果我选中一个用户并点击关闭会话。我需要终止该用户会话。要做到这一点,我需要有可访问的会话 – Ashish451

相关问题