How to check for session in JSP EL?

前端 未结 3 1870
忘掉有多难
忘掉有多难 2020-12-07 03:01

How do you check if a session exists for the request in EL? I\'m trying something like:

 ... <         


        
3条回答
  •  一生所求
    2020-12-07 03:41

    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!):

    
        

    The session has been created before.

    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.

    
        

    You've already visited this site before.

    You've just started the session with this request!

    (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:

    
        

    You're still logged in.

    You're not logged in!

提交回复
热议问题