Vladimir
Vladimir

Reputation: 1694

In JSP, how can i check, using JSTL, if certain session attribute exists in request?

This is code in servlet:

HttpSession session = request.getSession(true);
session.setAttribute("user", user);

I am forwarding request to JSP, where i want to check if there is session scoped user parameter attached.

<c:if test="${??? - check if user is attached to request}">
/   /message
</c:if>

Upvotes: 24

Views: 44100

Answers (3)

TheDude
TheDude

Reputation: 119

I think you mean checking the session scope right?

<c:if test="${!empty sessionScope.user}">

Upvotes: 10

Nidhish Krishnan
Nidhish Krishnan

Reputation: 20741

you can do that by using the following code

Setting session in Servlet

HttpSession session = request.getSession(); 
session.setAttribute("user", user);

Access session value by EL in JSP

<p>${sessionScope:user}</p>

Checking the session in JSP using JSTL

<c:if test="${sessionScope:user != null}" > 
   session value present......
</c:if>

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691635

<c:if test="${sessionScope.user != null}">
    There is a user **attribute** in the session
</c:if>

Upvotes: 37

Related Questions