Is it not possible to directly invoke session.getAttribute() with EL?

238 views Asked by At

The value for 'email' is set within session scope using

session.setAttribute("email", "[email protected]").

To display the value later, the following code was used

<c:out value='${session.getAttribute("email")}'> </c:out>

This yielded no output on the screen. Note that the necessary packages were imported and that there is no error produced.

However, if I re-write using the scriptlet: <% out.print(session.getAttribute("email")); %>, I get my desired output.

Why doesn't session.getAttribute() work when using the EL expression?

2

There are 2 answers

0
underdog On BEST ANSWER

My question is why doesn't session.getAttribute() work when using the JSTL tag?

The correct way of fetching a session attribute in a jsp via EL is

<c:out value='${sessionScope.EMAIL}'/>

You are messing up the JSTL & scriptlet code, you can try

email: <%= session.getAttribute("EMAIL") %>
1
DSF On

In EL you access session variables using the sessionScope object like ${sessionScope.EMAIL}.