'Converting HttpSession to base64

I'd like to know if there is any way I can encode an HttpSession object to Base64 String, as it is a non-serializable object. My approach is to have a Hashmap<String, Object> to put there not the session itself but its attributes and then encode that Hashmap. I need a way to persist this session object to I can recover it later on the program. I have something like:

        Enumeration<String> keys = session.getAttributeNames();
        Map<String, Object> sessionAttr = new HashMap<String, Object>();
        
        while(keys.hasMoreElements()) {         
            String key = keys.nextElement();
            Object attr = session.getAttribute(key);
            sessionAttr.put(key, obj);
        }
        /*And now I'd like to encode that HashMap to Base64 and return that String*/

It would be great if anyone could bring me some light over this problem and how to solve it in a better way.

Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source