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 getAllBann开发者_运维问答ers() 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");
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"
精彩评论