Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get session attribute with a dynamic key in EL?

Tags:

java

jsp

jstl

el

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?

like image 829
Nur Aini Avatar asked Nov 29 '11 10:11

Nur Aini


People also ask

How do I create a session attribute?

The client can create session attributes in a request by calling either the PostContent or the PostText operation with the sessionAttributes field set to a value. A Lambda function can create a session attribute in a response.

What is session attribute?

The session attribute indicates whether or not the JSP page uses HTTP sessions. A value of true means that the JSP page has access to a builtin session object and a value of false means that the JSP page cannot access the builtin session object.


2 Answers

<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:

  • Our EL wiki page
  • Java EE 6 tutorial - Examples of EL expressions
like image 53
BalusC Avatar answered Oct 25 '22 18:10

BalusC


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}" />
like image 40
Stanislav Levental Avatar answered Oct 25 '22 19:10

Stanislav Levental