4

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.

Tony R
  • 11,224
  • 23
  • 76
  • 101

3 Answers3

13

It's indeed never null. The session is always present in JSP EL, unless you add

<%@page session="false" %>

to top of JSP. You could then check for 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'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.

<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 brace notations for new are mandatory because new 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

session.setAttribute("user", user);

then you should rather be intercepting on that instead:

<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
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 2
    You're too good, answering a question I didn't even ask. Checking for the user was what I wanted. – Tony R Feb 03 '12 at 22:03
  • You're welcome. Questions which makes no sense in real world code have often a completely different functional requirement behind. Then I often guess for the most common functional requirements which look much like whatever you *really* need to achieve. You would save me (and others) time if you just state that in your future questions ;) – BalusC Feb 03 '12 at 22:10
  • It's sometimes easy to get caught up in a coding storm and forget to take a step back and realize the approach is wrong altogether. Thanks again. – Tony R Feb 03 '12 at 22:49
1

Seems to works with:

<c:if test="${fn:length(sessionScope) > 0}">

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)?

Tony R
  • 11,224
  • 23
  • 76
  • 101
1

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

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189