How do you check if a session exists for the request in EL? I'm trying something like:
<c:if test="${pageContext.request.session != null}"> ... </c:if>
but it seems like it's never null.
How do you check if a session exists for the request in EL? I'm trying something like:
<c:if test="${pageContext.request.session != null}"> ... </c:if>
but it seems like it's never null.
In J2EE there will always be a session object when a user visits a site.
What is a Session ? A session is pretty much what it sounds, when a user makes a page request to the server, the server creates a temporary session to identify that user. So when that same user goes to another page on that site, the server identifies that user. So a session is a small and temporary unique connection between a server and the user enabling it to identify that user across multiple page requests or visits to that site.
So basically if your hitting a page you have a session because your using JSP, which eventually gets converted to servlets.
http://www.stardeveloper.com/articles/display.html?article=2001062001&page=1
It's indeed never
null
. The session is always present in JSP EL, unless you addto top of JSP. You could then check for the session as follows (EL 2.2 only!):
I'm not sure what's the concrete functional requirement is. If you'd like to check if the session is new or has already been created, use
HttpSession#isNew()
instead.(the brace notations for
new
are mandatory becausenew
is a reserved literal in Java language)Of if you're relying on a specific session attribute, such as the logged-in user which is been set as
then you should rather be intercepting on that instead:
Seems to works with:
I wonder if there's a better way, since this requires that I have session attributes (I always do, but it's not really clean)?