开发者

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"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜