B.Z.B
B.Z.B

Reputation: 471

Possible to Store a HashMap in the ServletContext?

Is it possible to store a HashMap into the ServletContext in Java? When I go to get the attribute back it's a String...is there a way to cast it back to a HashMap? Its technically a

HashMap<Integer,ArrayList<byte[]>>

I Set the Attribute like this:

event.getServletContext().setAttribute("Banners", getAllBanners());

The method getAllBanners() returns a HashMap<Integer, ArrayList<byte[]>> object. Then when I want to access it I call:

event.getServletContext().getInitParameter("GBPBanners");

EDIT**

Got it HashMap <Integer, ArrayList<byte[]>> myMap = (HashMap<Integer,ArrayList<byte[]>>) event.getServletContext().getAttribute("Banners");

Upvotes: 0

Views: 3710

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340763

Works for me:

ServletContext ctx = request.getServletContext();
ctx.setAttribute("map", Collections.singletonMap(7, "Seven"));

//And later...

Map<Integer, String> map = (Map<Integer, String>) ctx.getAttribute("map");
String value = map.get(7);    //"Seven"

Upvotes: 4

Related Questions