开发者

removing data from session

开发者 https://www.devze.com 2022-12-27 10:08 出处:网络
I am using the following code to remove a attribute from session List<User> l=(List<User>) sess.getAttribute(\"allUserslist\");

I am using the following code to remove a attribute from session

List<User> l=(List<User>) sess.getAttribute("allUserslist");
for(int ii=0;ii<l.size();ii++){
    System.out.println("bef"+l.get(ii).getLoginName()+optionSelected[i]);
    if(l.get(ii).getLoginName().equals(optionSelected[i])){
   开发者_运维问答     System.out.println("in iteration");
        sess.removeAttribute(l.get(ii).getLoginName());
        List<User> l1=(List<User>) sess.getAttribute("allUserslist");
        System.out.println("final size"+l1.size());
    }
}

The final size after removing is still one as before, where am I going wrong?


Your code is removing the attribute named after the user, but it does not remove the user from the list. To do so:

List<User> l=(List<User>) sess.getAttribute("allUserslist");
for(int ii=0;ii<l.size();){
    System.out.println("bef"+l.get(ii).getLoginName()+optionSelected[i]);
    if(l.get(ii).getLoginName().equals(optionSelected[i])){
        System.out.println("in iteration");
        l.remove(ii)); // Remove user from the list
    } else {
        ++ii;
    }
}

List<User> l1=(List<User>) sess.getAttribute("allUserslist");
System.out.println("final size"+l1.size());
0

精彩评论

暂无评论...
验证码 换一张
取 消