Vishal Anand
Vishal Anand

Reputation: 591

How can I check for a presence of a logged in user in a session using JSTL?

I want to check if the user is logged in or not using JSTL.

I tried the following code:

<c:if test="${sessionScope.logged_in == null}">
      <c:redirect url="index.jsp"></c:redirect>
</c:if>


I have also tried the following code but it throws an IllegalStateException:

<c:if test="${empty user}">
    <b>you are not logged in</b>
<c:redirect url="index.jsp"></c:redirect>
</c:if>

but it is not working.

How can I implement the following code in JSTL:

<%
   String logged_in = (String) session.getAttribute("logged_in");
   if (logged_in == null) {
        response.sendRedirect("index.jsp");
   }
%>

Upvotes: 2

Views: 1386

Answers (1)

Alex
Alex

Reputation: 11579

Try this:

<c:if test="${empty logged_in}">
<c:redirect url="index.jsp"></c:redirect>
</c:if>

Upvotes: 3

Related Questions