-1

I am trying following code When ever a user login I want to store his userid in application scope. like

Map application = ActionContext.getContext().getApplication();
                Set<Long> logins = (Set<Long>) application.get("logins");
                if (logins == null) {
                    application.put("logins", logins);
                } else {
                    Set app = (Set<Long>) application.get("logins");
                    app.add(userid);
                    application.put("logins", app);
                }
                Set<Long> logins1 = (Set<Long>) application.get("logins");
              for(long l:logins1){
                    System.out.println(" "+l);
                }

and whenever user logged out I want to remove its id from application variable.

Problem above code is not working for my purpose how to achieve this?

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
xrcwrn
  • 5,339
  • 17
  • 68
  • 129
  • Define *not working*. Also consider [using a `@Singleton` EJB if in Java EE 6+](http://stackoverflow.com/q/26733141/1654265), or a Spring Singleton Bean (or whatever) if not. – Andrea Ligios Jan 08 '16 at 09:04

2 Answers2

1

Below code seems to work. But of course, if the user discards the session or session times out the user will still be counted as logged in.

private String user;
private List<Long> ids;
private List<Long> getUsers(){
    Map app = (Map) ActionContext.getContext().get("application");
    return app.get("users")==null? new ArrayList<Long>():(List<Long>) app.get("users");
}
private void putUsers(List<Long> users){
    Map app = (Map) ActionContext.getContext().get("application");
    app.put("users",users);
    return;
}
public String login(){
    Long userId = 0l;
    if(StringUtils.isNumeric(user)){
        userId=  Long.valueOf(user);
    }
    if(userId >0){
        ids = getUsers();
        if(!ids.contains(userId)){
            ids.add(userId);
            putUsers(ids);
        }
    }

    return "success";
}
public String logout(){
    Long userId = 0l;
    if(StringUtils.isNumeric(user)){
        userId=  Long.valueOf(user);
    }
    if(userId >0){
        ids = getUsers();
        if(ids.contains(userId)){
            ids.remove(userId);
            putUsers(ids);
        }
    }
    return "success";
}
public String status(){
    ids = getUsers();
    return "success";
}

public List<Long> getIds() {
    return ids;
}

public void setIds(List<Long> ids) {
    this.ids = ids;
}

public String getUser() {
    return user;
}

public void setUser(String user) {
    this.user = user;
}
Vasco
  • 782
  • 1
  • 5
  • 22
0

Don't you think that you should initialize the "logins" variable when it is null. Instead of

if (logins == null) 
  { 
              application.put("logins", logins); 
  } 

Try using this:

if (logins == null) 
  { 
              login = new Set<>(); 
              application.put("logins", logins); 
  } 

You havn't specified any error or what you wanna do. Please be specific for further help.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • 1
    You might be interested in googling how to format code on StackOverflow. You''ve put a hell of unnecessary effort in adding &nbps;,
    etc... but they're not needed at all. Just put FOUR spaces and it becomes code, or use the `{}` code button of the editor, or use the thick \` (Alt+96 on Win, Alt+' on Linux, in my locale) for inline code snippets.
    – Andrea Ligios Jan 08 '16 at 08:58
  • I was missing this part `login = new Set<>(); ` Now it is working – xrcwrn Jan 11 '16 at 07:39