How to get session attribute with a dynamic key in

2019-04-03 23:01发布

If I set session like this:

<% 
session.setAttribute("taintedAttribute", "what ever we want");
%>

normally we can get session variable like this in EL

${sessionScope.taintedAttribute }

But how about if I want to do like this

<% 
String name = "taintedAttribute";
//session.setAttribute(name, "what ever we want");
session.getAttribute(name);
%>

Then how can we call it in EL?

Can EL get something like ${sessionScope.---dynamic name ---}?

If I do this:

<c:set var="name" value="taintedAttribute" />
<c:out value="${sessionScope.[name]}"/>

the name will be replaced by taintedAttribute as the same as this line

${sessionScope.taintedAttribute}

Is that possible? How can I do that?

标签: java jsp jstl el
2条回答
时光不老,我们不散
2楼-- · 2019-04-03 23:27
<c:set var="name" value="taintedAttribute" />
<c:out value="${sessionScope.[name]}"/>

You were close. Remove the period.

<c:set var="name" value="taintedAttribute" />
<c:out value="${sessionScope[name]}"/>

See also:

查看更多
闹够了就滚
3楼-- · 2019-04-03 23:27

Look at http://www.java2s.com/Code/Java/JSTL/JSTLSetVariablesScope.htm

<c:set var="test" value="Session Level Value"
    scope="session" />
<c:out value="${sessionScope.test}" />
查看更多
登录 后发表回答