简体   繁体   中英

Fail to retrieve value from HashMap with HttpSession as key

I have a HashMap where I am putting HttpSession object as Key and HashSet as Value

HashMap<HttpSession, HashSet<String>> map = new HashMap<HttpSession, HashSet<String>>();

I have a HttpSession listener where I need to check weather session object is exist in the Map

public void sessionDestroyed(HttpSessionEvent se) {
    HashMap<HttpSession, HashSet<String>> map = //Get map object
    HttpSession session = se.getSession();
    if(map.containsKey(session)){
        //TODO Code goes here
    }
}

IF condition always return false, even though session object is available in the map (manually checked the map entries). I am stuck!!!

If the value was put in the map in a previous request, the HttpSession object that you have now is not necessarily the same that is present in the map (even though they represent the same session). Two representations of the same session are not required (AFAIK) to be equals or to have the same hashCode , which will break the map.

You may try using the session.getId() value as key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM