It really is never null . A session is always present in JSP EL , unless you add
<%@page session="false" %>
to the top of the JSP. Then you can check the session as follows (EL 2.2 only):
<c:if test="${pageContext.request.getSession(false) != null}"> <p>The session has been created before.</p> </c:if>
I am not sure what a specific functional requirement is. If you want to check if the session is new or has already been created, use HttpSession#isNew() .
<c:if test="${not pageContext.session['new']}"> <p>You've already visited this site before.</p> </c:if> <c:if test="${pageContext.session['new']}"> <p>You've just started the session with this request!</p> </c:if>
(the binding notation for new is mandatory, since new is a reserved literal in Java)
If you rely on a specific session attribute, for example, a registered user that has been set as
session.setAttribute("user", user);
then you better intercept this:
<c:if test="${not empty user}"> <p>You're still logged in.</p> </c:if> <c:if test="${empty user}"> <p>You're not logged in!</p> </c:if>
Balusc
source share