Search code examples
jspelsession-scope

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


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

session.setAttribute("email", "john@gmail.com").

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?


Solution

  • 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") %>