Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value of key within Map in Java

I'm not at Java developer but I'm trying to figure out how to get a value from a Map. I'm using a basic (old) Struts app with no fancy stuff or JSTL afaik. I can get all the key/value pairs to output by converting the Map to a String:

<% String myValue = pageContext.getSession().getAttribute("myMap").toString(); %>

However, when I try to access a specific key it doesn't work:

<% String myValue = pageContext.getSession().getAttribute("myMap['myKey']").toString(); %>
like image 671
skube Avatar asked Sep 13 '26 02:09

skube


1 Answers

<% String myValue = ((Map) pageContext.getSession().getAttribute("myMap")).get("myKey").toString(); %>
resp.
<% String myValue = ((Map) session.getAttribute("myMap")).get("myKey").toString(); %>
// because session is an implicit object in JSP

However, I would strongly discourage you from using such spaghetti code in JSP. If you need to output the "myKey" value, you can use e.g. <c:out> tag with EL:

<c:out value="${session.myMap.myKey}" />
or
<c:out value="${session['myMap']['myKey']" />
like image 184
Jozef Chocholacek Avatar answered Sep 14 '26 15:09

Jozef Chocholacek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!