• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

How to get session object into a hashmap

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am saving a HashMap into a session like so:

   

But how am I supposed to get it out of the session back into a HashMap ?

I am aware that the session data is stored as an object but do not know how to get it into a `HashMap`

I've tried:

   

But it says

cannot from Object to HashMap

 
Saloon Keeper
Posts: 7026
164
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to cast it to the correct type, like this:

(HashMap<Integer, Cart>) session.getAttribute("cart")
 
Vojislav Kovacevic
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any way to get rid of unchecked warning when I do this?

I am using @SuppressWarnings("unchecked"), is there a better option in your opinion?
 
Tim Moores
Saloon Keeper
Posts: 7026
164
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case, no. The servlet session API deals only in Objects.
 
Saloon Keeper
Posts: 13061
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a natural consequence of session attributes not being type-safe. Suppressing the warning is fine if you know for sure that your application always assigns the same type to that attribute.

What's more problematic is your naming. It looks like your Cart class encapsulates a single entry of a shopping cart. So why is it called Cart and not something like CartEntry?

If you write a separate Cart class that encapsulates a Map<String, CartEntry> (after all, why are IDs represented by int? Are you going to use arithmetic on them?) you can cast the attribute to Cart and you won't get a warning:
 
Vojislav Kovacevic
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephen,

It's just the logic I went with on the fly.

The ids are there as keys to separate each product (which is what the Cart class contains).

I will think about what you said and try to implement it and see if it's a better solution.

I am always open to better code!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic